June 2018
Beginner
288 pages
6h 31m
English
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. |
Before we declare const as one of the most awesome features in modern JavaScript, let’s understand its limitations. ...