使用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的值

相关文章:

  • 2021-09-17
  • 2022-12-23
  • 2021-11-10
  • 2021-08-14
  • 2022-12-23
  • 2021-05-20
  • 2022-12-23
猜你喜欢
  • 2021-04-09
  • 2021-10-10
  • 2021-08-25
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案