Javascript Object.isFrozen()

The Object.isFrozen() method checks if an object is frozen.

Example


isFrozen() syntax

The syntax of the isFrozen() method is:

Object.isFrozen(obj)

Here, isFrozen() is a static method. Hence, we need to access the method using the class name, Object.

Note: A frozen object can no longer be changed. Freezing an object prevents:

  • New properties from being added to the object.
  • Existing properties to be removed from the object.
  • Changing the enumerability, configurability, or writability of existing properties.
  • Changing values of the existing object properties and prototype.

isFrozen() Parameters

The isFrozen() method takes in:

  • obj - the object which should be checked for being frozen or not.

isFrozen() Return Value

The isFrozen() method returns:

  • true - if the object is frozen
  • false - if the object is not frozen

Example: Javascript Object.isFrozen()

In the above example, we tried to freeze newObj by

  • using the preventExtensions() method, and
  • making it unwritable by setting the writable flag to false.

However, both approaches fail to freeze the object.

The final output indicates that newObj is frozen only after calling the freeze() method.


Recommended Reading: