This is an experimental technology, part of the Harmony (ECMAScript 6) proposal.
Because this technology's specification has not stabilized, check the compatibility table for usage in various browsers. Also note that the syntax and behavior of an experimental technology is subject to change in future version of browsers as the spec changes.
Summary
The const declaration creates a read-only named constant.
Syntax
const name1 = value1 [, name2 = value2 [, ... [, nameN = valueN]]]];
-
nameN
- Constant name. It can be any legal identifier.
-
valueN
- Value of the constant. It can be any legal expression.
Description
This declaration 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.
Examples
The following example demonstrates how constants behave. Try this in you browser console. In Firefox and Chrome you will see this behavior:
// define my_fav as a constant and give it the value 7 const my_fav = 7; // this will fail silently in Firefox and Chrome (but does not fail in Safari) my_fav = 20; // will print 7 console.log("my favorite number is: " + my_fav); // trying to redeclare a constant will either throw and error, of fail silently const my_fav = 20; // the name my_fav is reserved for constant above, so this will also fail var my_fav = 20; // my_fav is still 7 console.log("my favorite number is " + my_fav);
While almost all browsers today allow you to declare named constants, not all of them (notably Safari) respect the fact that it should be read only. Therefore, while you can use constants as a way of indicating to other developers that you are creating a constant instead of a variable, currently you can not rely on all browsers to enforce this and prevent the rest of the code from changing your constant.
Specifications
Specification | Status | Comment |
---|---|---|
ECMAScript 6 (ECMA-262) The definition of 'Let and Const Declarations' in that specification. |
Draft | Initial definition. |
Browser compatibility
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | ? | ? | 11 | 12.00 (maybe earlier) | 5.1.7 (maybe earlier) |
Reassignment fails |
20 | 13 (13) | ? | ? | ? |
Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | ? | ? | ? | ? | ? | ? |
Reassignment fails | ? | ? | ? | ? | ? | ? |
Notes
The current implementation of const
in Firefox 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 var
).
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 13 and Chrome (at least since version 20).
const
is defined in ECMAScript 6 (see above), but with different semantics. Similar to variables declared with the let
statement, constants declared with const
will be block-scoped, do not hoist, and throw a SyntaxError exception if code attempts to access the binding before it has been declared.