0Pricing
JavaScript Academy · Lesson

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

  1. Object.freeze and seal
  2. Shallow vs Deep Freezing
  3. Immutable Update Patterns
  4. structuredClone for Deep Copies
← Back to JavaScript Academy