The const keyword

The ES6 const keyword is used to declare the read-only variables, that is, the variables whose value cannot be reassigned. Before ES6, the programmers usually used to prefix the variables that were supposed to be constant. For example, take a look at the following code:

var const_pi = 3.141;
var r = 2;
console.log(const_pi * r * r); //Output "12.564"

The value of pi should always remain constant. Here, although we have prefixed it, there is still a chance that we might accidentally change its value somewhere in the program, as they're no native protection to the value of pi. Prefixing is just not enough to keep the track of the constant variables.

Therefore, the const keyword was introduced to provide a native protection to the ...

Get React: Building Modern Web Applications 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.