1.var声明的变量会挂载在window上
var a = 10;
console.log(a,window.a) //10 10
let b = 10;
console.log(b,window.b) //10 undefined
let c = 10;
console.log(c,window.c) //10 undefined
2.var声明的变量存在变量提升
console.log(a)
var a = 10;
console.log(a)
//undefined
//10
其执行过程相当于
var a
console.log(a)
a = 10;
console.log(a)
console.log(a);
let a = 10;
Uncaught ReferenceError: Cannot access 'a' before initialization
初始化前无法访问a
const和let一样
3、let和const声明的变量会形成作用域
if (true) {
var a = 10
let b = 20
}
console.log(a) //10
console.log(b) // b is not defined
const和let一样
4、同一作用域下let和const不能声明重名变量
vara = 100;
console.log(a); // 100
var a = 10;
console.log(a); // 10
let a = 100;
let a = 10;
//Identifier 'a' has already been declared
//标识符“a”已经被声明
const和let一样
5、const一经声明必须赋值
const a
//Missing initializer in const declaration
// const声明中缺少初始化程序
6、变量一经const声明不能修改
const a = 10;
a = 20;
console.log(a)
// Assignment to constant variable.
//分配给常数变量。
但是const声明的数组、对象可以修改
const a = [ ]
a[0] = 20
const b = { }
b.name = "张三"
console.log(a) // [20]
console.log(b.name) // 张三