May 2019
Intermediate to advanced
316 pages
7h 13m
English
Constants, or immutable variables, can be defined by using a const keyword, as shown in the following code. When using the const keyword, the variable content cannot be reassigned:
const PI = 3.14159;
The scope of const is block scoped, which is the same as the scope for let. This means that the const variable can only be used inside the block in which it is defined. In practice, the block is the area between curly brackets { }. The following sample code shows how the scope works:
var count = 10;if(count > 5) { const total = count * 2; console.log(total); // Prints 20 to console}console.log(total); // Error, outside the scope
The second console.log statement gives an error because we are trying to use the total variable ...
Read now
Unlock full access