JavaScript(JS) object method - isSealed

The isSealed() method is a built-in method of the JavaScript Object constructor. It returns a boolean indicating whether the specified object is sealed, i.e., whether its properties cannot be added or deleted, although their values can be changed.

Here's the syntax:

re‮ref‬ to:theitroad.com
Object.isSealed(obj)

where obj is the object to be checked. This method takes only one argument, which is the object to be checked.

Here's an example that shows how to use the isSealed() method:

const myObj = { prop1: "value1", prop2: "value2" };
console.log(Object.isSealed(myObj)); // false

Object.seal(myObj);
console.log(Object.isSealed(myObj)); // true

delete myObj.prop1;
console.log(myObj); // { prop1: "value1", prop2: "value2" }

In this example, we create an object myObj with two properties prop1 and prop2. We use the isSealed() method to check whether the object is sealed. Since the object is not sealed yet, isSealed() returns false.

Next, we use the Object.seal() method to seal the object. We then use the isSealed() method again to check whether the object is sealed. Since we have sealed the object, isSealed() now returns true.

Finally, we try to delete the prop1 property of the object, but it remains unchanged since the object is sealed. When we log the value of myObj, we see that it still contains both properties.

The isSealed() method is useful for checking whether an object is sealed or not. If an object is sealed, you cannot add or delete its properties, but you can still modify their values.