Shallow vs Deep Freezing
Understand how far freezing reaches.
Freeze Is Shallow
Object.freeze only locks the top level of an object. Any nested object or array it references remains fully mutable.
This surprises many developers and causes subtle bugs.
Proving the Shallow Limit
Freeze an object with a nested object. The top level is locked, but the nested object can still change.
const user = Object.freeze({
name: 'Ada',
address: { city: 'London' }
});
user.name = 'Grace';
user.address.city = 'Paris';
console.log(user.name);
console.log(user.address.city);All lessons in this course
- Object.freeze and seal
- Shallow vs Deep Freezing
- Immutable Update Patterns
- structuredClone for Deep Copies