Declares a read-only named constant.
|ECMAScript Edition||none. (ES.next)|
const name1 = value1 [, name2 = value2 [, name3 = value3 [, ... [, nameN = valueN]]]];
- Constant name. It can be any legal identifier.
- Value of the constant. It can be any legal expression.
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.
The current implementation of
const is a Mozilla-specific extension and is not part of ECMAScript 5. It is supported in Firefox & Chrome (V8). As of Safari 5.1.7 and Opera 12.00, if you define a variable with
const in these browsers, you can still change its value later. It is not supported in Internet Explorer 6-10, but is included in Internet Explorer 11. The
const keyword currently declares the constant in the function scope (like variables declared with
Firefox, at least since version 13, throws a TypeError if you redeclare a constant. None of the major browsers produce any notices or errors if you assign another value to a constant. The return value of such an operation is that of the new value assigned, but the reassignment is unsuccessful only in Firefox and Chrome (at least since version 20).
const is going to be defined by ECMAScript 6, but with different semantics. Similar to variables declared with the
let statement, constants declared with
const will be block-scoped.
The following example produces the output "
a is 7."
const a = 7; console.log("a is " + a + ".");