公共特性
1. 无变量声明提升
console.log(a) // undefinedvar a = 123
console.log(a) // ReferenceError: a is not defined.const a = 123
2. 不能重复声明
var a = 123const a = 123 // Uncaught SyntaxError: Identifier 'a' has already been declare
const a = 123const a = 123 // Uncaught SyntaxError: Identifier 'a' has already been declare
3. 块级作用域
概念
任何一对花括号({和})中的语句集都属于一个块,在这之中定义的所有变量在代码块外都是不可见的
{var a = 123}console.log(a) // 123
{const a = 123}console.log(a) // ReferenceError: a is not defined.
4. 暂时性死区
概念
代码块内,使用
let命令声明变量之前,该变量都是不可用的作用
为了减少运行时错误,防止在变量声明前就使用这个变量,从而导致意料之外的行为 ```javascript var tmp = 123;
if (true) { tmp = ‘abc’; // ReferenceError let tmp; }
<a name="t0t9o"></a># 5. 不会挂载到顶层对象 window```javascriptvar a = 123console.log(window.a) // 123
const b = 123console.log(window.b) // undefined
