ES6关键字
1、let
作用:声明变量
特点:1.没有变量提升
例: function fn1(){
console.log(num);
let num =1;
}
fn1(); //Cannot access 'num' before initialization
2.有块级作用域
例: for(let i = 0;i<5;i++){
// var num = 10;//Identifier 'num' has already
let num = 5;
console.log(num);
}
console.log(num); //num is not defined
3.函数内不能用let重新声明函数的参数
例: function fnn(name){
let name = '李四';
console.log(name); //Identifier 'name' has already
}
fnn('张三');
2、const
作用:声明常量
特点:1、不允许重复声明同一个常量名。
例: var num = 10;
const num = 5;
console.log(num);//Identifier 'num' has already been declared
2、声明常量必须赋值。
例:const num;
console.log(num);// Missing initializer in const declaration
3、常量不可修改。
例:const num = 10;
num = 5;
console.log(num);//Assignment to constant variable.
4、没有常量提升。
例:function fn() {
console.log(num);//Assignment to constant variable.
const num = 10;
}
5、具有块级作用域。
例:for (var i = 0; i < 5; i++) {
const num = 10;
}
console.log(num);// num is not defined