1.let,const在代码块内有效,var是在全局范围内有效
{
let test1 = 0;
var test2 = 1;
}
test1 // test1is not defined
test2 // 1
2.let 和const只能声明一次,var 可以声明多次
let test1 = 1;
let test1 = 2;
var test2 = 3;
var test2 = 4;
test1 // Identifier 'test1 ’ has already been declared
test2 // 4
3.var声明变量存在变量提升,let和const不存在变量提升
console.log(a); //ReferenceError: a is not defined
let a = “apple”;
console.log(b); //undefined
var b = “banana”;
console.log©; //ReferenceError: c is not defined
const c = “cstrawberry”;
4.const 声明一个只读变量,声明之后不允许改变。意味着,一旦声明必须初始化,否则会报错。
const PI = “3.1415926”;
PI // 3.1415926
const MY_AGE; // SyntaxError: Missing initializer in const declaration
以上均为学习总结,欢迎补充