const

Declares a block-scoped binding that cannot be reassigned.

Since ES2015 (ES6) Spec ↗

Syntax

const name = value;
const name1 = value1, name2 = value2;

Returns

undefined — `const` is a statement, not an expression — it does not produce a value.

Throws

  • SyntaxError — A `const` is declared without an initializer (`const x;`), or the same name is declared twice in the same block.
  • TypeError — A `const` binding is reassigned after declaration.
  • ReferenceError — A `const` binding is accessed before its declaration runs (the **temporal dead zone**).

Examples

const PI = 3.14159;
console.log(PI);
Output
3.14159
// Reassignment is an error
const greeting = "hi";
// greeting = "hello"; // TypeError: Assignment to constant variable.
console.log(greeting);
Output
hi
// const objects are NOT deeply immutable
const user = { name: "Ada" };
user.name = "Lin"; // OK — mutating a property is fine
console.log(user.name);
Output
Lin
// const must be initialized
// const empty; // SyntaxError: Missing initializer in const declaration

Notes

- **Default to `const`.** A `const` declaration tells the reader "this name will keep pointing at the same value" — one less thing to track when reading code. - `const` makes the *binding* constant, not the *value*. To prevent object mutation, use `Object.freeze()`. - Like `let`, `const` is block-scoped.

Browser & runtime support

EnvironmentSince version
chrome 49
firefox 36
safari 11
edge 14
node 6.0

See also