//var 没有块级作用域的概念 var x = 1 { var x = 2 } console.log(x)//2 //let const有块级作用域 let y = 1 { //这个声明被限制在块级作用域内部 let y = 2 console.log('let2->',y)//2 } console.log('let1->',y)//1 //const 和var 一样 由于有块级作用域的存在,重复声明不会报错 const z = 1 { const z = 2 console.log('const2->',z) } console.log('const1->',z) //如果没有块作用域重复声明会发生什么?
var k = 1
var k = 2//没有任何错误
let m = 1 let m = 2//报错 Uncaught SyntaxError: Identifier 'm' has already been declared // const n = 1 // const n =2//Uncaught SyntaxError: Identifier 'n' has already been declared