JavaScript const Keyword

前端 未结 8 1681
自闭症患者
自闭症患者 2020-12-09 13:25

Does the const keyword in JavaScript create an immutable reference to immutable data structures? [I\'m assuming that immutable data structures exist in JavaScri

8条回答
  •  遥遥无期
    2020-12-09 14:02

    Yes that is right. A const would only declare a read-only named constant. Changing its value will have no effect.

    Reference for const on MDN:

    Const creates a constant that can be global or local to the function in which it is declared. Constants follow the same scope rules as variables.

    The value of a constant cannot change through re-assignment, and a constant cannot be re-declared. Because of this, although it is possible to declare a constant without initializing it, it would be useless to do so.

    A constant cannot share its name with a function or a variable in the same scope.

    Here's the browser compatibility matrix.

提交回复
热议问题