JS: const Declaration

By Xah Lee. Date: . Last updated: .

New in JS2015.

const is for declaring constants. It's similar to let Declaration, except you can't change its value once set.

const name = val;
declare and assign.
const name1 = val1, name2 = val2, name3 = val3 etc;
declare and assign multiple constants
// variable declared with const cannot be changed
const x = 3;
x = 4; // TypeError: Assignment to constant variable.

const Must Have a Value When Declared

const must have a value when declared.

// const must have a value
const x; // SyntaxError: Missing initializer in const declaration
x = 3;

Constant of Array

if a Array value is declared constant, the array itself can still change.

const ar = [3,4];
ar . push (5);
console.log ( ar );

Constant of Object

If a Object value is declared const, you can still add/delete/change properties of the object.

// if object is declared constant, the object properties can still be modified
const ob = {};
ob.p = 3;
console.log (ob); // { p: 3 }

Note, you can prevent add/delete/change properties by using Object.seal

No Name Hoisting

The const doesn't have name hoisting as var does. That means, you can't use the name before its const declaration.

[see var Declaration Order]

Buy JavaScript in Depth

JavaScript in Depth

JS Obj Ref


JavaScript in Depth

Basic Syntax

Value Types





Object and Inheritance



Iterable 🌟

Regular Expression


Set Object

Map Object