const
is excellent. It allows us to keep our variables in controlled scope, and reduces errors caused by the case of mistaken reassignments.
But it can also lead to a false sense of security. Not everything is truly immutable when defined by const
.
Consider this simple example -
|
|
This is applicable for other types of variables, or to simpler objects created on top of these primitives.
|
|
But the behaviour is not as expected when you are dealing with strings or objects. You cannot reassign value, sure - but you can change existing value.
|
|
You can create an immutable object but it needs an additional step.
|
|
The same is applicable for other types of complex objects as well.
|
|