JavaScript Const

ECMAScript 2015

ES2015 introduced two important new JavaScript keywords:let and const.

Through const defined let Variables defined are similar to

Example

const PI = 3.141592653589793;
PI = 3.14;      // This will cause an error
PI = PI + 10;   // This will also cause an error

Try Yourself

block scope

inblock scopeused inside const The declared variable is similar to let Variables are similar.

In this example, x is declared in the block, which is different from x declared outside the block:

Example

var x = 10;
// Here, x is 10
{ 
  const x = 6;
  // Here, x is 6
}
// Here, x is 10

Try Yourself

In the previous chapter JavaScript Let Learn more about block scope in Chapter 2.

Assigned at the time of declaration

JavaScript const Variables must be assigned a value at the time of declaration:

Incorrect

const PI;
PI = 3.14159265359;

Correct

const PI = 3.14159265359;

Not a true constant

Keyword const It is somewhat misleading.

It does not define a constant value. It defines a constant reference to the value.

Therefore, we cannot change the original value of a constant, but we can change the properties of the constant object.

Original value

If we assign an original value to a constant, we cannot change the original value:

Example

const PI = 3.141592653589793;
PI = 3.14;      // This will cause an error
PI = PI + 10;   // This will also cause an error

Try Yourself

Constant objects can be changed

You can change the properties of constant objects:

Example

// You can create const objects:
const car = {type:"porsche", model:"911", color:"Black"};
// You can change properties:
car.color = "White";
// You can add properties:
car.owner = "Bill";

Try Yourself

However, you cannot reassign a value to a constant object:

Example

const car = {type:"porsche", model:"911", color:"Black"};
car = {type:"Volvo", model:"XC60", color:"White"};    // ERROR

Try Yourself

Constant arrays can be changed

You can change elements of a constant array:

Example

// You can create a constant array:
const cars = ["Audi", "BMW", "porsche"];
// You can change elements:
cars[0] = "Honda";
// You can add elements:
cars.push("Volvo");

Try Yourself

However, you cannot reassign a value to a constant array:

Example

const cars = ["Audi", "BMW", "porsche"];
cars = ["Honda", "Toyota", "Volvo"];    // ERROR

Try Yourself

Browser support

Internet Explorer 10 or earlier versions do not support const Keyword.

The following table defines the first browser versions that fully support the const keyword:

Chrome 49 IE / Edge 11 Firefox 36 Safari 10 Opera 36
March 2016 October 2013 February 2015 September 2016 March 2016

Re-declaration

It is allowed to re-declare JavaScript at any position in the program var Variable:

Example

var x = 2;    // Allowed
var x = 3;    // Allowed
x = 4;        // Allowed

It is not allowed to re-declare an existing variable within the same scope or block: var or let Re-declaring or reassigning a variable to const:

Example

var x = 2;         // Allowed
const x = 2;       // Not allowed
{
  let x = 2;     // Allowed
  const x = 2;   // Not allowed
}

Re-declaring or assigning a value to an existing const variable within the same scope or block is not allowed:

Example

const x = 2;       // Allows
const x = 3;       // Not allowed
x = 3;             // Not allowed
var x = 3;         // Not allowed
let x = 3;         // Not allowed
{
  const x = 2;   // Allows
  const x = 3;   // Not allowed
  x = 3;         // Not allowed
  var x = 3;     // Not allowed
  let x = 3;     // Not allowed
}

to be declared again in another scope or block const Is allowed:

Example

const x = 2;       // Allows
{
  const x = 3;   // Allows
}
{
  const x = 4;   // Allows
}

Hoisting

Through var Declared variables areHoistingto the top. If you are not familiar with hoisting, please learn about hoisting in this chapter.

You can use a var variable before declaring it:

Example

carName = "Volvo";    // You can use carName here
var carName;

Try Yourself

Through const Declared variables are not hoisted to the top.

const Variables cannot be used before they are declared:

Example

carName = "Volvo";    // You cannot use carName here
const carName = "Volvo";