定义变量let,const

1.块级作用域let

声明变量,作用域是最近的“{}”;

'use strict';
{
    let test = '1';
}
console.log(test);//test is not defined
  1. 恒量const

const 声明的恒量不能改变;

'use strict';
const test = 1;
test = 2;//Assignment to constant variable.

但是可以向恒量定义的数据结构中添加内容:

'use strict';
const test = [];
test.push(1);
console.log(test);//[ 1 ]

你可能感兴趣的:(定义变量let,const)