块级作用域

 重复定义会报错

if(true){
    let a = 1;
    let a = 2;     
}

输出:

Uncaught SyntaxError: Identifier 'a' has already been declared

 

嵌套循环不会相互影响

 for (let i = 0; i < 3; i++) {
        console.log("out", i);
        for (let i = 0; i < 2; i++) {
            console.log("in", i);
        }
    }

结果 out 0 in 0 in 1 out 1 in 0 in 1 out 2 in 0 in 1

 

 实现块级作用域

if(true){
    let name = '123';
}
console.log(name);

 

 

不存在变量提示

console.log('inner',i);
    let i = 100;

输出;

Uncaught ReferenceError: Cannot access 'i' before initialization

 

posted @ 2019-08-09 10:50  1点  阅读(188)  评论(0编辑  收藏  举报