JavaScript Reference ยท Object
Object.seal()
Reference for JavaScript Object.seal(): lock an object's set of properties so none can be added or removed while values stay writable.
Object.seal() locks the shape of an object. After sealing, you cannot add new properties, delete existing ones, or reconfigure them (for example, turn a data property into an accessor). Values of writable properties can still change.
Think of it as Object.preventExtensions() plus "every existing property becomes non-configurable". It sits between that call and Object.freeze(), which also makes values read-only.
Object.seal(obj); Object.isSealed(obj); // true
Try it
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Syntax
Object.seal(obj)
| Name | Description |
|---|---|
obj |
The object to seal. |
Return value: the same object that was passed in. Primitive arguments are returned as they are.
Exceptions: none for the call itself. Operations that violate the seal throw a TypeError in strict mode and fail silently otherwise.
Usage notes
- Permanent and shallow. A sealed object cannot be unsealed, and nested objects are not sealed. Seal them separately if you need a deep lock.
- Writable stays writable. Sealing sets
configurable: falseon every own property but leaveswritablealone. A writable property can still be assigned; a writable data property can also be switched to read-only withObject.defineProperty(), which is the one allowed change. - Arrays can be sealed. Elements can be replaced, but
push()andpop()fail (they throw aTypeErrorbecause they try to add or remove elements). - Sealed is also non-extensible.
Object.isExtensible()returnsfalse. - Frozen objects are sealed.
Object.isSealed()istruefor any frozen object. - When to use it: a fixed record whose fields should keep updating, such as a state object with a known list of keys. For a constant, use
Object.freeze(). - Performance: sealing is cheap, and engines can sometimes optimize objects whose shape cannot change, but do not rely on that.
Browser support
Object.seal() is available in every browser and JavaScript runtime.