const

The const keyword is used to define a variable whose value shouldn’t change. If you intend to modify the value in a variable, then define it using let; otherwise, define it using const.

Here’s an example that shows the difference between using let and const:

 //BROKEN CODE
 'use strict'​;
 let​ price = 120.25;
 const​ tax = 0.825;
 
 price = 110.12;
 
 tax = 1.25;

There’s no issue changing the value of the price variable. However, since tax is defined as a constant, we will get a runtime error when we try to modify the value:

 tax = 1.25;
  ^
 
 TypeError: Assignment to constant variable.

Reach of const

Before we declare const as one of the most awesome features in modern JavaScript, let’s understand its limitations. ...

Get Rediscovering JavaScript now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.