[ES6] 03. The let keyword -- 1

var message = "Hi";
{
   var message = "Bye";       
}

console.log(message); 

//Bye

The message inside the block still has impact on the outside.

Just remember that something like for, while if ... block, they don't create new scope. function block do create new scope!

 

If you add function around the inside message:

复制代码
var message = "Hi";

function greeting(){
    var message = "Bye";
}

console.log(message);

//Hi
复制代码

Then "Bye" message has no impact on the "Hi" message.

But if create something like "for", "while" loop and if block, you will still get the "Bye";

 

Let


 

To help with this problem, we do have LET in ES6, which will allow me to use block scoping.

let message = "Hi";
{
    let message = "Bye";
}

console.log(message);

//Hi

This "Bye" message, because it's inside of a block, even though it's not inside of a function, has no impact on the assignment of this message. They are two separate and different entities.

So we con consider that "let" keyword will create a new scope in the current block!

 

Let in For Loop:


 

Recall one problem code:

复制代码
var fs = [];

for(var i = 0; i < 10; i++){
    fs.push(function(i){
         console.log(i)   
    });
}

fs.forEach(function(f){
    f();
});

//10
//10
...
//10
复制代码

The output will be 10 all the time.

 

If we swtich var to let:

复制代码
var fs = [];

for(let i = 0; i < 10; i++){
    fs.push(function(i){
         console.log(i)   
    });
}

fs.forEach(function(f){
    f();
});

//0
//1
...
//9
复制代码

Then the output is 0-9. The reason for that is because, let keyword each time it create a new instance in for loop.

 

What this really means in the end is that if you're used to bringing your variables up to the top of a scope using VAR and things like VAR i, VAR temp, where you want to be careful, because you're afraid of wasting behaviors due to this i.

复制代码
function varFun(){
    
    var previous = 0;
    var current = 1;
    var i;
    var temp;

    for(i = 0; i < 10; i++){
          temp = previous;
          previous = current;
          current = temp + current;
    }
}


function letFun(){

    let previous = 0;
    let current = 1;

    for(let i = 0; i < 10; i++){
        let temp = previous;
        previous= current;
        current = temp + current;
    }
}
复制代码

Feel free now to use the LET keyword, and instead of declaring it at the top, you can declare it in line, inside of the FOR statement, as well as declaring it inside of the FOR block, and it'll safely create this temp each time it goes through the FOR block.

 

Using let instead of var prevents variable declarations from being moved to the top of the scope on what is known as hositing.

posted @   Zhentiw  阅读(295)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示