console.log(b) // Cannot access 'b' before initialization
let b = 2
console.log(c) // Cannot access 'c' before initialization
const c = 3
复制代码
块级作用域
var
复制代码
不存在块级作用域
let
复制代码
和
const
复制代码
存在块级作用域
{ var a = 2}console.log(a) // 2{ let b = 2}console.log(b) // Uncaught ReferenceError: b is not defined{ const c = 2}console.log(c) // Uncaught ReferenceError: c is not defined
复制代码
重复声明
var
复制代码
在相同作用域下允许重复声明,后面声明的变量会覆盖前面的变量声明
let
复制代码
、
const
复制代码
在相同作用域下不允许重复声明
var a = 10
var a = 20 // 20
let b = 10
let b = 20 // Identifier 'b' has already been declared
const c = 10
const c = 20 // Identifier 'c' has already been declared