Recently, with the departure of ECMAScript 2015 , the use of the reserved word was introduced, const
which I understand is for constants.
I have tried it with some variables and everything works very well, like this:
'use strict';
const PI = 3.141516;
PI = 3.14;
console.log(PI);
Which returns the following error:
TypeError: Assignment to constant variable.
That is completely expected in a constant, but my doubt arises when I do the same thing with an object, like so:
const empleado = {
nombre: 'Juan',
profesion: 'Programador'
};
empleado.nombre = 'Pedro';
console.log(empleado);
Which to my surprise returns the object with the nombre
modified node, like this:
{ nombre: 'Pedro', profesion: 'Programador' }
Ask
Shouldn't I get the same error when trying to modify an object that has been declared as a constant?
Because you are confusing the concept of a constant reference with an immutable object.
Reference (in this case a constant) is simply a memory address which points to an object. Instead the object is the value to which the constant refers.
In this example:
The value of the constant
empleado
(a reference that points to a particular object) remains unchanged, that is, it continues to point to the same object. What has changed is the value ofempleado.nombre
not that ofempleado
Instead:
If it gives an error because it is trying to modify the value of the constant
empleado
If you want to make the object immutable you should use the
Object.freeze()
Example:
This time it is no longer allowed to modify the object to which the variable refers
empleado
, but you will be able to modify the variableempleado
since it is not a constant.Of course you can combine both
In this way you would be preventing you from changing the reference to the object and neither its properties.
More information about constants in JavaScript in this answer to the question in Advantages of using const over var in JavaScript
This means that you cannot override the object:
because it marks you
TypeError: Assignment to constant variable.
However, the attributes of the object are not protected. So, as in your example, this statement can be executed without problems:
If you want to have a totally constant object you can use the following function:
In your example it would look like this:
For more information you can see (in English):
In several documentations it is already explained that this cannot be done with variables:
But you can do this with arrays:
But not this:
When you mark something as a constant you are not making the object unable to change its value, but you are making the variable pointers not vary, where they point. However, you can give these variables other values in memory as long as the memory space in which they are stored does not change.
An example: If you generate a constant object named First with two variables A1 and B1, and another named Second with variables A2 and B2. If you match them and do "First = Second", you are actually changing the pointers from A1 and B1 to A2 and B2 respectively, and that is why you get the exception. However you can set A1 to the value of A2 without problem, because you will be changing the in-memory value of the pointer of First.A1, but not the pointer itself.
To take a more hypothetical look at this old question. Suppose you
const
declare immutable constants as the OP expects.What would happen with the following code?
Of course the correct answer is simpler:
var , let , and const declare variable or constant identifiers . They do not create objects or primitive values that happens on the right side of the
=
.And, of course, const means that the constant is the identifier, not the object associated with it.