const

时间:2020-03-18 15:16:56   收藏:0   阅读:53

 const声明常量,只读、值不能改变。

const PI = 3.1415;
 PI = 3;
// Uncaught TypeError: Assignment to constant variable.

 声明必须赋值 

 const PI
 //Uncaught SyntaxError: Missing initializer in const declaration

 有块级作用域

if (true) {
    const PI = 5;
}
PI // Uncaught ReferenceError: PI is not defined

 没有变量提升

if (true) {
  console.log(PI); //Uncaught ReferenceError: Cannot access ‘PI‘ before initialization
  const PI = 3;
}

 不可重复声明

const a = 1;
const a = 2; // Uncaught SyntaxError: Identifier ‘a‘ has already been declared

 

原文:https://www.cnblogs.com/blogZhao/p/12517073.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!