What is the use of Object.isFrozen() method in JavaScript?



Object.isFrozen()

Object.isFrozen() method is used to find whether an object is frozen or not. 

An object is frozen if it followed the below criteria

  • It should not be extensible.

  • Its properties should be non-configurable.

  • It shouldn't accept any new properties.

Syntax

Object.isFrozen(obj);

Parameters - Object.isFrozen() accepts an object as a parameter and scrutinizes whether it is frozen or not and returns a boolean output.

Example

In the following example Object.isFrozen() scrutinizes whether the object 'obj' is frozen or not. Since the object is not frozen, false will be displayed as the output.

 Live Demo

<html>
<body>
<script>
   var object = {
      prop1 : 5
   }
   var res = Object.isFrozen(object);
   document.write(res);
</script>
</body>
</html>

Output

false

Example

In the following example since the object 'object' is frozen by using Object.freeze(), true will be displayed as the output.

Live Demo

<html>
<body>
   <script>
      var object = {
         prop1 : 5
      }
      Object.freeze(object);
      var res = Object.isFrozen(object);
      document.write(res);
   </script>
</body>
</html>

Output

true
raja
Published on 26-Jun-2019 15:45:30
Advertisements