const

诪讬诇转 讛讛爪讛专讛 const聽诪砖诪砖转 诇讛讻讝专讛 注诇 诪砖转谞讛聽拽讘讜注聽砖讗讬谉 讗驻砖专讜转聽诇砖谞讜转 讗转 讛注专讱 砖诇讜.

转讞讘讬专

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.

转讗讬诪讜转 讚驻讚驻谉

Update compatibility data on GitHub
DesktopMobileServer
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung InternetNode.js
constChrome Full support 21Edge Full support 12Firefox Full support 36
Notes
Full support 36
Notes
Notes Prior to Firefox 13, const is implemented, but re-assignment is not failing.
Notes Prior to Firefox 46, a TypeError was thrown on redeclaration instead of a SyntaxError.
IE Full support 11Opera Full support 9Safari Full support 5.1WebView Android Full support 鈮37Chrome Android Full support 25Firefox Android Full support 36
Notes
Full support 36
Notes
Notes Prior to Firefox 13, const is implemented, but re-assignment is not failing.
Notes Prior to Firefox 46, a TypeError was thrown on redeclaration instead of a SyntaxError.
Opera Android Full support 10.1Safari iOS Full support 6Samsung Internet Android Full support 1.5nodejs Full support 6.0.0

Legend

Full support
Full support
See implementation notes.
See implementation notes.

专讗讛 讙诐