至于为什么开始学习ES6,主要是因为小程序,因为小程序目前很好的支持ES6
一、常量const
const num = 520;
console.log(PI);
num = 3; // 报错: Uncaught TypeError: Assignment to constant variable.
const num = 3.1; // 报错: Uncaught SyntaxError: Identifier "num" has already been declared
一旦声明以后就不能更改了,区别于var,只在声明的块级中有效。
二、作用于let
function test(){
let x=1;
let y=2;
{
let x=10;
let y=20;
}
console.log(x,y)
}
test();//输出结果,是x=1,y=2