← Back to JavaScript, page 1

javascript-0020 · Medium mid

What is the difference between Object.freeze, Object.seal, and Object.preventExtensions?

preventExtensions stops new properties from being added. seal also stops properties from being deleted or reconfigured, but existing values can still change. freeze also makes existing values read-only. All three are shallow, so nested objects stay changeable.

Full answer

These three methods lock an object in three increasing steps.

Object.preventExtensions(obj) forbids adding new properties. Existing properties can still be changed or deleted.

Object.seal(obj) does that and also marks every existing property as non-configurable, so none can be deleted or turned into an accessor. Values of writable properties can still change.

Object.freeze(obj) does all of that and also makes every data property non-writable, so values cannot change either. It is the strongest of the three.

In sloppy mode, a forbidden change fails silently. In strict mode, which includes modules and classes, it throws a TypeError. Use Object.isFrozen, Object.isSealed, and Object.isExtensible to check the state.

All three are shallow. A frozen object that holds an array or another object still allows that inner value to change, unless it is frozen too. A deep freeze needs a recursive function. Arrays can be frozen as well, which blocks push, pop, and index assignment.

Freezing is useful for constants, configuration, and catching accidental mutation. const is different: it protects the variable binding, not the object.

javascript
'use strict';

function attempt(label, action) {
  try {
    action();
    console.log(label, 'allowed');
  } catch (error) {
    console.log(label, 'blocked', error.name);
  }
}

const fixed = Object.preventExtensions({ a: 1 });
attempt('preventExtensions add:', () => { fixed.b = 2; });
attempt('preventExtensions change:', () => { fixed.a = 5; });
attempt('preventExtensions delete:', () => { delete fixed.a; });

const sealed = Object.seal({ a: 1 });
attempt('seal add:', () => { sealed.b = 2; });
attempt('seal change:', () => { sealed.a = 5; });
attempt('seal delete:', () => { delete sealed.a; });

const frozen = Object.freeze({ a: 1, nested: { count: 0 }, list: [1] });
attempt('freeze change:', () => { frozen.a = 5; });
attempt('freeze nested change:', () => { frozen.nested.count = 1; });
attempt('freeze nested array push:', () => { frozen.list.push(2); });
attempt('frozen array push:', () => { Object.freeze([1]).push(2); });

console.log(Object.isFrozen(frozen), Object.isSealed(frozen), Object.isExtensible(frozen));
console.log(Object.isFrozen(frozen.nested));

Run with Node.js v24.22.1 as js-20.js; two runs gave identical output. Output: preventExtensions add: blocked TypeError preventExtensions change: allowed preventExtensions delete: allowed seal add: blocked TypeError seal change: allowed seal delete: blocked TypeError freeze change: blocked TypeError freeze nested change: allowed freeze nested array push: allowed frozen array push: blocked TypeError true true false false

Key points

  • preventExtensions: no new properties.
  • seal: no new, no delete, no reconfigure; values still writable.
  • freeze: nothing can be added, deleted, or changed.
  • All three are shallow; nested objects need their own freeze.
  • Violations throw TypeError in strict mode and fail silently otherwise.

Common mistakes

  • Assuming freeze is deep.
  • Confusing const with freeze. const stops reassignment of the name only.
  • Relying on silent failure in sloppy mode and missing that the change was ignored.
  • Freezing large objects in hot code paths without measuring the cost.

Follow-ups

  • How would you write a deepFreeze function, and how do you handle cycles?
  • What do writable, enumerable, and configurable mean in a property descriptor?
  • How does Object.freeze interact with a class with private fields?

Tips

  • Present the three as steps: prevent adding, then also prevent deleting, then also prevent changing.
  • Always say "shallow" and demonstrate it with a nested object.
  • Mention strict mode behavior; it is the reason many demos seem to do nothing.