暂时性死区#

如果区块中存在letconst命令,这个区块对这些命令声明的变量,从一开始就形成了封闭作用域。凡是在声明之前就使用这些变量,就会报错。

1
2
3
4
5
6
let a = 1;
// 块级作用域
{
a = 2; // Cannot access 'a' before initialization
let a = 3;
}

变量提升#

变量可以在var声明之前使用,值为undefined

1
2
console.log(a); // undefined
var a = 1;
1
2
console.log(a); // Cannot access 'a' before initialization
let a = 1;

重复声明#

  • var可重复声明

  • let不可重复声明

1
2
3
var a = 1;
var a = 2;
console.log(a); // 2
1
2
let a = 1;
let a = 2; // Identifier 'a' has already been declared

作用域#

1
2
3
4
{
var a = 1;
}
console.log(a); // 1
1
2
3
4
{
let a = 1;
}
console.log(a); // ReferenceError: a is not defined

全局对象#

node环境中

1
2
3
4
5
6
x = "哈哈哈哈";
var y = "呵呵呵呵";
let z = "嘿嘿嘿嘿";
console.log(global.x); // "哈哈哈哈"
console.log(global.y); // undefined
console.log(global.z); // undefined

brower环境中

1
2
3
4
5
6
x = "哈哈哈哈";
var y = "呵呵呵呵";
let z = "嘿嘿嘿嘿";
console.log(window.x); // 哈哈哈哈
console.log(window.y); // 呵呵呵呵
console.log(window.z); // undefined

总结let的属性特征

  • 不属于全局对象window /global

  • 不允许重复声明

  • 不存在变量提升

  • 存在暂时性死区

  • 块级作用域

const#

  • const声明的变量必须初始化

  • 且不能修改值, 只读(对于对象类型的数据, 可修改其属性, 但不能修改其地址, 即整体赋值)

  • 作用域与let基本相同