Computer >> Computer tutorials >  >> Programming >> Javascript

How to know whether an object is sealed or not in JavaScript?


Object.isSealed() is the method used to find whether an object is sealed or not in javascript. This method gives out boolean output. 

An object is sealed if the following conditions hold true.

1) It should not be extensible.

2) its properties should be non-configurable.

Syntax

Object.isSealed(obj)

Arguments -  Object.isSealed() takes an object as a parameter and returns a boolean value based on whether object is sealed or not.

Example

In the following example since the object is not sealed we will get false as the output. To seal an object the method called "Object.seal()" must be used.

<html>
<body>
<script>
   var obj = {
      prop1: 2
   }
   var res = Object.isSealed(obj);
   document.write(res);
</script>
</body>
</html>

Output

false

Example

In the following example the object 'obj' is sealed using 'Object.seal()' method. So when 'Object.isSealed()' is used, true will be displayed as the output.

<html>
<body>
<script>
   var obj = {
      prop1: 2
   }
   Object.seal(obj);
   var res = Object.isSealed(obj);
   document.write(res);
</script>
</body>
</html>

Output

true