ES6基础之——恒量const

使用const能够去声明一个衡量,这样就不可以给这个衡量去从新分配一个新的值

 

例子:
const fruit ="apple";
console.log(fruit);

const fruit ="lemon";
console.log(fruit);

 

在控制台上会报语法错误:
Uncaught SyntaxError: Identifier 'fruit' has already benn declared at <anonymous>:1:1

 

注意:const限制的是给const分配值的动做,并非限制衡量里面的值
 
例子:
const fruit =[];
fruit.push('apple');
fruit.push('lemon');
console.log(fruit); //[apple,lemon]

 

若是从新给fruit分配一个值:
fruit=[];
console.log(fruit); //Uncaught SyntaxError: Assignment to constant variable.

 

这就是由于咱们从新分配了fruit的值
相关文章
相关标签/搜索