JavaScript Object.isSealed()

The Object.isSealed() method checks if an object is sealed or not.


// by default, objects are not sealed
let empty = {};

// Output: false

isSealed() syntax

The syntax of the isSealed() method is:


The isSealed() method, being a static method, is called using the Object class name.

isSealed() Parameters

The isSealed() method takes in:

  • obj - the object which should be checked

isSealed() Return Value

The isSealed() method returns a Boolean indicating whether the given object is sealed or not. It returns:

  • true - if obj is sealed
  • false - if obj is not sealed

Note: The sealed object is not extensible, which means we cannot add new properties and delete the existing properties in the object.

However, the sealed object may or may not be writable. If the sealed object is writable prior to using the seal() method, then it remains writable and vice-versa.

Example 1: JavaScript Object.isSealed() With Empty Object

// objects are not sealed by default
let empty = {};

// check if empty object is sealed

// Output: false

// non-extensible empty objects are sealed

// check if empty object is sealed

// Output: true

Example 2: isSealed() With Non-Empty Object

// create a non-empty object
let obj = { value: "A B C" };

// non-empty objects are sealed only when
// all its properties are non-configurable

// check if obj is sealed

// Output: false

// make all properties non-configurable
Object.defineProperty(obj, "value", { configurable: false });

// check if obj is sealed

// Output: true

As can be seen from the example above, simply using the preventExtensions() method won't guarantee that the object will be sealed. To seal an object, we also need to ensure that all its properties are non-configurable.

Hence, we get false as an output when using the isSealed() method on obj, even though we've already used preventExtensions() on it.

Therefore, we have used the defineProperty() method on obj to set the configurable flag of its property to false. This ensures that obj is finally sealed.

Example 3: isSealed() With the seal() Method

let obj2 = { a: 1, b: 2, c: 3 };

// seal the obj2 object
// using the seal() method

// check if obj2 is sealed console.log(Object.isSealed(obj2));
// Output: true

Using the seal() method always ensures that the object is sealed. Hence, we get true as an output after using the seal() method with the obj2.

Also Read:
Did you find this article helpful?