Object.freeze() is useful to prevent an object from changing, so no new properties can be added to it, no existing properties can be removed, the enumerability can not be changed, and existing values of properties can not be changed.
Notice that the boolean property of registered did not change because we froze
the address object.
It is important to note that even if an object is frozen and not open to mutation
it does not mean that it is constant since freeze is shallow meaning
that deeper child objects can actually be mutated:
If we wanted the entire object to be frozen we would have to do a deep freeze.
We talked about deep and shallow operations in our javascript cloning post.
Mozilla has a deepFreeze function example:
Also note that Object.freeze does allow for reassignment if we don’t use a const
for that method: