Object.seal()
One of three levels of lock-down. preventExtensions blocks additions; seal also blocks deletions; freeze also blocks writes. Seal is the one people reach for least and confuse most.
Demo
The pair shows sealed and frozen side by side. For an object with properties the answer is [true, false] — sealed, because nothing can be added or removed, but not frozen, because those properties can still be assigned to. The empty-object case returns [true, true], which looks like a bug and is not: an object with no properties has no writable properties, so sealing it satisfies the definition of frozen vacuously. Any predicate you write over possibly-empty objects has to account for that.
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
| object | any | yes | The object to seal. Primitives are returned unchanged and report as sealed, as with freeze. |
Return value
object — The SAME object, sealed — no properties can be added or deleted, but existing ones can still be written to.
Common patterns
const state = Object.seal({count: 0, name: ""});
Object.preventExtensions(obj);
Object.preventExtensions(o); // no adds Object.seal(o); // no adds, no deletes Object.freeze(o); // no adds, deletes or writes
Examples
Pitfalls
const o = Object.seal({a: 1}); o.a = 99; o.a
const o = Object.freeze({a: 1});
Object.isFrozen(Object.seal({}))
Object.isSealed(o) && Object.keys(o).length > 0
const o = Object.seal({a: 1}); o.b = 2; o.b
"use strict"; Object.seal({}).b = 2
const o = Object.seal({n: {}}); o.n.added = 1; o.n
Object.values(o).forEach(v => typeof v === "object" && Object.seal(v));
When to use
- Fixing an object shape while allowing values to change — state containers
- Catching typo-ed property names, which would otherwise silently create a new key
- A middle ground where freeze would be too strict
- You want read-only → freeze
- You only need to block additions → preventExtensions
- Nested data matters → neither is deep
- You want immutable updates → build new objects instead
Notes
FAQ
A ladder. preventExtensions blocks new properties but allows deleting and writing. seal adds a block on deletion. freeze adds a block on writing. Every frozen object is sealed and non-extensible; the reverse does not hold.
Object.preventExtensions(o); // no adds Object.seal(o); // + no deletes Object.freeze(o); // + no writes