How to Declare Constants in JavaScript?
ES6 provides a new way of declaring a constant by using the const keyword which creates a read-only reference to a value:
const CONSTANT_VALUE = "someValue";
This works in almost all browsers.
Local variables with block scope can be declared with const. Assuming, we have a constant variable CONSTANT_VALUE. After declaring a value like a constant variable, you can’t assign a new value to it:
The constant cannot change through re-assignment constant cannot be re-declared. Const can be changed if it is an object because when you're adding to an array or object you're not re-assigning or re-declaring the constant, it's already declared and assigned, you're just adding to the "list" that the constant points to. So this works fine:
The const Variable
ES2015 introduced two new JavaScript keywords: let and const. Variables defined with const behave like let variables, except they cannot be re-assigned. In the programming world, a constant is something that does not change. The const creates a constant that is a read-only reference to a value, which does not mean the value it holds is immutable. It only indicates that the variable identifier cannot be re-assigned.