As one old teacher of mine would say:
Do not confuse "arquipelago de Fernando de Noronha" with "skinny smoking marijuana" . They may sound alike, but they are completely different.
He said this basically in the sense that not everything is what it seems.
The term const
in JavaScript defines a constant reference to an object. That is, the reference can not be overwritten. Once the reference points to an object, it can not receive another object.
const number = 2;
number = 3;
Note that attempting to overwrite the object with number
gives error. But being constant is different from being unchangeable. An immutable object can not mutate, and if it does, it defines another object. For example, if I have the integer, which is immutable, 1, and increment it in a unit I will have object 2, which is independent of the original 1. Therefore, even assuming it as constant, I can not modify it:
const number = 1;
number++;
The same error will be displayed because the object will be constant and immutable, unlike an array that is a changeable type. Even by assigning it a constant reference, the object continues to be mutable, allowing it to mutate without representing another object:
const arr = [];
arr.push(1);
console.log(arr);
MDN documentation comments on:
The const
statement creates a read-only reference to a value. This does not mean that this value is immutable, only that the constant variable identifier can not be changed. If the content of the identifier is an object, the parameters of that object can be changed, for example.
Other interesting readings:
If you prefer something more official: