Object.seal()
BaselineWidely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
TheObject.seal()
static methodseals an object. Sealing an objectprevents extensions and makes existing properties non-configurable. A sealed object has a fixed set of properties: new properties cannot be added, existing properties cannot be removed, their enumerability and configurability cannot be changed, and its prototype cannot be re-assigned. Values of existing properties can still be changed as long as they are writable.seal()
returns the same object that was passed in.
Try it
const object1 = { property1: 42,};Object.seal(object1);object1.property1 = 33;console.log(object1.property1);// Expected output: 33delete object1.property1; // Cannot delete when sealedconsole.log(object1.property1);// Expected output: 33
Syntax
Object.seal(obj)
Parameters
obj
The object which should be sealed.
Return value
The object being sealed.
Description
Sealing an object is equivalent topreventing extensions and then changing all existingproperties' descriptors toconfigurable: false
. This has the effect of making the set of properties on the object fixed. Making all properties non-configurablealso prevents them from being converted from data properties to accessor properties andvice versa, but it does not prevent the values of data properties from being changed.Attempting to delete or add properties to a sealed object, or to convert a data propertyto accessor or vice versa, will fail, either silently or by throwing aTypeError
(most commonly, although not exclusively, when instrict mode code).
Private elements are not properties and do not have the concept of property descriptors. Private elements cannot be added or removed from the object, whether the object is sealed or not.
The prototype chain remains untouched. However, due to the effect ofpreventing extensions, the[[Prototype]]
cannot be reassigned.
UnlikeObject.freeze()
, objects sealed withObject.seal()
may have their existingproperties changed, as long as they are writable.
Examples
Using Object.seal
const obj = { prop() {}, foo: "bar",};// New properties may be added, existing properties// may be changed or removed.obj.foo = "baz";obj.lumpy = "woof";delete obj.prop;const o = Object.seal(obj);o === obj; // trueObject.isSealed(obj); // true// Changing property values on a sealed object// still works.obj.foo = "quux";// But you can't convert data properties to accessors,// or vice versa.Object.defineProperty(obj, "foo", { get() { return "g"; },}); // throws a TypeError// Now any changes, other than to property values,// will fail.obj.quaxxor = "the friendly duck";// silently doesn't add the propertydelete obj.foo;// silently doesn't delete the property// … and in strict mode such attempts// will throw TypeErrors.function fail() { "use strict"; delete obj.foo; // throws a TypeError obj.sparky = "arf"; // throws a TypeError}fail();// Attempted additions through// Object.defineProperty will also throw.Object.defineProperty(obj, "ohai", { value: 17,}); // throws a TypeErrorObject.defineProperty(obj, "foo", { value: "eit",}); // changes existing property value
Non-object argument
In ES5, if the argument to this method is not an object (a primitive), then it will cause aTypeError
. In ES2015, a non-object argument will be returned as-is without any errors, since primitives are already, by definition, immutable.
Object.seal(1);// TypeError: 1 is not an object (ES5 code)Object.seal(1);// 1 (ES2015 code)
Specifications
Specification |
---|
ECMAScript® 2026 Language Specification # sec-object.seal |