Our volunteers haven't translated this article into ไทย yet. Join us and help get the job done!
let
statement. The value of a constant cannot change through re-assignment, and it can't be redeclared. Syntax
const name1 = value1 [, name2 = value2 [, ... [, nameN = valueN]]];
nameN
- The constant's name, which can be any legal identifier.
valueN
- The constant's value; this can be any legal expression.
Description
This declaration creates a constant that can be either global or local to the function in which it is declared. An initializer for a constant is required; that is, you must specify its value in the same statement in which it's declared (which makes sense, given that it can't be changed later).
The const
declaration creates a read-only reference to a value. It does not mean the value it holds is immutable, just that the variable identifier cannot be reassigned. For instance, in case the content is an object, this means the object itself can still be altered.
All the considerations about the "temporal dead zone" that apply to let
, also apply to const
.
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 your browser console.
// NOTE: Constants can be declared with uppercase or lowercase, but a common // convention is to use all-uppercase letters. // define MY_FAV as a constant and give it the value 7 const MY_FAV = 7; // this will throw an error MY_FAV = 20; // will print 7 console.log("my favorite number is: " + MY_FAV); // trying to redeclare a constant throws an error const MY_FAV = 20; // the name MY_FAV is reserved for constant above, so this will also fail var MY_FAV = 20; // this throws an error also let MY_FAV = 20; // it's important to note the nature of block scoping if (MY_FAV === 7) { // this is fine and creates a block scoped MY_FAV variable // (works equally well with let to declare a block scoped non const variable) const MY_FAV = 20; // MY_FAV is now 20 console.log("my favorite number is " + MY_FAV); // this gets hoisted into the global context and throws an error var MY_FAV = 20; } // MY_FAV is still 7 console.log("my favorite number is " + MY_FAV); // Assigning to A const variable is a syntax error const A = 1; A = 2; // throws an error, missing initializer in const declaration const FOO; // const also works on objects const MY_OBJECT = {"key": "value"}; // Attempting to overwrie the object throws an error MY_OBJECT = {"OTHER_KEY": "value"}; // However, object keys are not protected, // so the following statement is executed without problem MY_OBJECT.key = "otherValue"; // Use Object.freeze() to make object immutable // The same applies to arrays const MY_ARRAY = []; // It's possible to push items into the array MY_ARRAY.push("A"); // ["A"] // However, assigning a new array to the variable throws an error MY_ARRAY = ["B"]
Specifications
Specification | Status | Comment |
---|---|---|
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Let and Const Declarations' in that specification. |
Standard | Initial definition. |
ECMAScript 2017 Draft (ECMA-262) The definition of 'Let and Const Declarations' in that specification. |
Draft |
Browser compatibility
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | 21 | (Yes) | 36 (36) | 11 | 12 | 5.1 |
Reassignment fails | 20 | (Yes) | 13 (13) | 11 | ? | 10.0 |
Allowed in sloppy mode | 49.0 |
Feature | Android | Android Webview | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile | Chrome for Android |
---|---|---|---|---|---|---|---|
Basic support | No support | (Yes) | ? | ? | (Yes) | ? | (Yes) |
Reassignment fails | No support | (Yes) | ? | ? | (Yes) | 10.0 | (Yes) |
Allowed in sloppy mode | No support | 49.0 | 49.0 |
Compatibility notes
In earlier versions of Firefox & Chrome and as of Safari 5.1.7 and Opera 12.00, if you define a variable with const
, you can still change its value later. It is not supported in Internet Explorer 6-10, but is included in Internet Explorer 11.
Firefox-specific notes
- Prior to SpiderMonkey 46 (Firefox 46 / Thunderbird 46 / SeaMonkey 2.43), a
TypeError
was thrown on redeclaration instead of aSyntaxError
(bug 1198833). - Starting with SpiderMonkey 36 (Firefox 36 / Thunderbird 36 / SeaMonkey 2.33):
{const a=1};a
now throws aReferenceError
and does not return1
anymore due to block-scoping.const a;
now throws aSyntaxError
("missing = in const declaration"
): An initializer is required.const a = 1; a = 2;
now also throws aSyntaxError
("invalid assignment to const a").