var:全局声明,会改变全局变量的值,进而污染全局变量
var a=2;
{
var a=3;
}
console.log(a);//3
let:局部声明,不会改变全局变量的值
var a=2;
{
let a=3;
}
console.log(a);//2
const:常量,声明了就不能改变
const a = 1;
var a = 2;
console.log(a);//编译报错
var a=2;
{
var a=3;
}
console.log(a);//3
var a=2;
{
let a=3;
}
console.log(a);//2
const a = 1;
var a = 2;
console.log(a);//编译报错