每天学一点,知识涨一张
var 默认是会变量提升的,变量可以修改;
let 定义变量,变量可以修;
const 定义必须有常量值,const的值一但写上不可更改;
let 与const相同之处:
1> 都是块级作用域,适用于 {},在括号外面打印里面的值会报错;
2> 变量不可提升。
3>暂时性死区(只要块级作用域内,存在let,const
,它所声明的变量就绑定(binding)在这个区域,不再受外部影响。)
举例说明
console.log(alls)//undefined ==》变量提升 var alls=123; //let console.log(alls_let)// ==》报错 let alls_let=123; function demo(){ let a=10; } console.log(a)//==》报错 //const console.log(alls_const)//====报错 const alls_const=10; const alls_count =11;//报错 function demo(){ const a=10; console.log(a)//10 } console.log(a)//==》报错