const
is a good practice for both readability and maintainability and avoids using magic literals e.g.
if (x > 10) {
// Better!
if (x > maxRows) {
}
const declarations must be initialized
Left hand side of assignment cannot be a constant
Constants are immutable after creation, so if you try to assign them to a new value it is a compiler error:
const foo = 123;
Block Scoped
A const
is block scoped like we saw with let
:
Deep immutability
foo = { bar: 456 }; // ERROR : Left hand side of an assignment expression cannot be a constant
However it still allows sub properties of objects to be mutated, as shown below:
For this reason I recommend using const
with primitives or immutable data structures.