constย ืืฉืืฉืช ืืืืืจื ืขื ืืฉืชื ืย ืงืืืขย ืฉืืื ืืคืฉืจืืชย ืืฉื ืืช ืืช ืืขืจื ืฉืื.ย
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
ืชืืืืจ
const name1 = value1 [, name2 = value2 [, ... [, nameN = valueN]]];
nameN- ืฉื ืืืฉืชื ื.
valueN- ืืขืจื ืฉื ืืืฉืชื ื.
ืชืืืืจ
ืืฉืคืช ื'ืืืื ืกืงืจืืคื ืื ื ืืฉืชืืฉืื ืืืฉืชื ืื ืขื ืื ืช ืืืืืืง ืขืจืืื ืฉืื ืื.
ืืฆืืจื ืขื ืืฉืชื ื ืืืืฆืขืืช const ืืืคืืช ืืืชื ืืงืืืข ืืื ื ืืชื ืืฉื ืืช ืืช ืืขืจื ืฉืื.
ืืงืฆืืช ืขืจื ืืืฉืชื ื ืืื ืืฆืืจื ืืจืืฉ ืืืคืืช ืืืชื ืืืฉืชื ื ืืืืืื, ืื ืืฉืื ื ืืืฆืืจื ืืืืฆืขืืช var ืืื ืืื ืย ืืคืืฃ ืืืืืืืงื ืืื window.
ืืฉืื ืืฆืืื ืฉืืฆืืจื ืืืืฆืขืืช const ืื ืืืฆืขืชย Hoisting.
ืืืืืืืช
ืืืืืื ืืืื ืืืืืฉื ืืืฆื ืืชื ืืืื ืืฉืชื ืื ืงืืืขืื.
// 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 - Uncaught TypeError: Assignment to constant variable.
MY_FAV = 20;
// MY_FAV is 7
console.log('my favorite number is: ' + MY_FAV);
// trying to redeclare a constant throws an error - Uncaught SyntaxError: Identifier 'MY_FAV' has already been declared
const MY_FAV = 20;
// the name MY_FAV is reserved for constant above, so this will fail too
var MY_FAV = 20;
// this throws an error too
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)
let 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);
// throws an error - Uncaught SyntaxError: Missing initializer in const declaration
const FOO;
// const also works on objects
const MY_OBJECT = {'key': 'value'};
// Attempting to overwrite the object throws an error - Uncaught TypeError: Assignment to constant variable.
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 - Uncaught TypeError: Assignment to constant variable.
MY_ARRAY = ['B'];
ืืคืจื
| Specification | Status | Comment |
|---|---|---|
| ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Let and Const Declarations' in that specification. |
Standard | Initial definition. |
| ECMAScript (ECMA-262) The definition of 'Let and Const Declarations' in that specification. |
Living Standard | No changes. |
ืชืืืืืช ืืคืืคื
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
const | Chrome Full support 21 | Edge Full support 12 | Firefox
Full support
36
| IE Full support 11 | Opera Full support 9 | Safari Full support 5.1 | WebView Android Full support โค37 | Chrome Android Full support 25 | Firefox Android
Full support
36
| Opera Android Full support 10.1 | Safari iOS Full support 6 | Samsung Internet Android Full support 1.5 | nodejs Full support Yes |
Legend
- Full support ย
- Full support
- See implementation notes.
- See implementation notes.

