美文网首页
ECMAScript6学习笔记-D2 const命令

ECMAScript6学习笔记-D2 const命令

作者: 福兮祸所依 | 来源:发表于2017-07-12 21:51 被阅读0次

    const命令用来声明变量,但是与let不同的是const声明的是常量。一旦声明,常量的值便不可更改

    const PI = 3.14159265857;
    console.log(PI);//3.14159265854
    PI = 3;
    console.log(PI);//3.14159265854
    const PI = 8;
    console.log(PI);
    //报错  Uncaught SyntaxError: Identifier 'PI' has already been declared
    

    const的作用域与let命令相同:只在声明所在的块级作用域内有效。

    if (condition) {
            const MAX = 5;
    }
    // 常量MAX在此处不可得
    //报错  Uncaught ReferenceError: condition is not defined
    

    const声明的变量同样不可以重复声明

     var message = "hello";
    let age = 25;
    //以下两行代码都会报错
    const message = "goodbye";
    const age = 30;
    

    相关文章

      网友评论

          本文标题:ECMAScript6学习笔记-D2 const命令

          本文链接:https://www.haomeiwen.com/subject/fgrihxtx.html