ES6新增语法--const

const(★★★)

声明常量,常量就是值(内存地址)不能变化的量

具有块级作用域

 if (true) { 
    const a = 10;
}
console.log(a) // a is not defined

声明常量时必须赋值

const PI; // Missing initializer in const declaration

常量赋值后,值不能修改

const PI = 3.14;
PI = 100; // Assignment to constant variable.

const ary = [100, 200];
ary[0] = 'a';
ary[1] = 'b';
console.log(ary); // ['a', 'b'];
ary = ['a', 'b']; // Assignment to constant variable.

小结

  • const声明的变量是一个常量

  • 既然是常量不能重新进行赋值,如果是基本数据类型,不能更改值,如果是复杂数据类型,不能更改地址值

  • 声明 const时候必须要给定值

posted @ 2020-10-19 23:12  清出于兰  阅读(218)  评论(0编辑  收藏  举报