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

JavascriptObject Oriented ProgrammingFront End Technology

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.

Live Demo

<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.

Live Demo

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

Output

true
raja
Published on 26-Jun-2019 13:44:44
Advertisements