1、const
声明常量,一旦声明必须立马赋值,否则报错
const PI = 3.14
const PI; //报错:Uncaught SyntaxError: Missing initializer in const declaration
常量赋值以后,不能改变,否则同样报错
const PI = 3.14 ; PI = 3; //报错:Uncaught TypeError: Assignment to constant variable.
2、let
声明的变量只在let命令所在的代码块内有效
for(let i=0 ;i<3;i++){ console.log(i) } console.log(i) //报错:Uncaught ReferenceError: i is not defined