JavaScript Object.isSealed()

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

Example


isSealed() syntax

The syntax of the isSealed() method is:

Object.isSealed(obj)

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


Example 2: isSealed() With Non-Empty Object

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

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.


Recommended Reading: