JavaScript Const


The word const started being used in JavaScript's ES6 version, which came out in 2015.

Variables that are set with const cannot be declared again.

Variables defined with const cannot be changed once they are set.

Variables that are set with const only exist within the block of code where they are defined.

Cannot be Reassigned

A const variable can't change its value once it's set:


Must be Assigned

In JavaScript, when you create a variable using the const keyword, you have to give it a value right when you make it:

Correct

const PI = 3.14159265359;

Incorrect

const PI;
PI = 3.14159265359;

When to use JavaScript const?

Whenever you're sure that a value won't change, use const to declare a variable.

Use const when you say:

  • A new Array
  • A new Object
  • A new Function
  • A new RegExp

Constant Objects and Arrays

The word const might confuse you a bit.

It doesn't set a fixed value. Instead, it sets a fixed way to refer to a value.

Because of this you can NOT:

  • Reassign a constant value
  • Reassign a constant array
  • Reassign a constant object

    But you CAN:

  • Change the elements of constant array
  • Change the properties of constant object

Constant Arrays

You have the power to modify the parts of an unchanging list:

But you can't change the array's contents:


Constant Objects

You have the ability to modify the characteristics of an unchangeable object:

But you cannot change the object:


Difference Between var, let and const

ScopeRedeclareReassignHoistedBinds this
varNoYesYesYesYes
letYesNoYesNoNo
constYesNoNoNoNo

What is Good?

In JavaScript, let and const keywords create variables with block scope.

let and const cannot be declared again.

Declare let and const before using them.

let and const don't connect to this.

let and const aren't hoisted.

What is Not Good?

var does not have to be declared.

var is hoisted.

var binds to this.


Block Scope

Creating a variable using const is much like using let when it comes to block scope.

The x stated inside this block isn't the same as the x declared outside of it.


Redeclaring

You can change a JavaScript variable declared with var at any point in your code.

Example

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

You can't change a variable from being var or let to const if it's already been declared in the same part of the code.

Example

var x = 2;     // Allowed
const x = 2;   // Not allowed

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

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

Changing a constant variable that already exists in the same area of a program isn't allowed.

Example

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

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

Changing the value of a variable using const is okay if done in a different part of the code or within a different section of the program.

Example

const x = 2;       // Allowed

{
  const x = 3;   // Allowed
}

{
  const x = 4;   // Allowed
}

Hoisting

Variables made with var go up to the top and can be set at any time.

Explanation: You're allowed to utilize the variable even if it hasn't been officially declared yet.

If you're interested in understanding hoisting better, check out the section on JavaScript Hoisting.

Variables made with const are lifted to the top but not set initially.

Meaning: If you try to use a const variable before declaring it, you'll get a ReferenceError: