JavaScript(JS) object method - isExtensible

w‮i.ww‬giftidea.com

The isExtensible() method is a built-in method of the JavaScript Object constructor. It returns a boolean indicating whether the specified object is extensible, i.e., whether new properties can be added to it.

Here's the syntax:

Object.isExtensible(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 isExtensible() method:

const myObj = {};
console.log(Object.isExtensible(myObj)); // true

Object.preventExtensions(myObj);
console.log(Object.isExtensible(myObj)); // false

In this example, we create an empty object myObj and use the isExtensible() method to check whether it is extensible. Since the object is empty, it is extensible by default, so isExtensible() returns true.

Next, we use the Object.preventExtensions() method to prevent any further properties from being added to the object. We then use the isExtensible() method again to check whether the object is still extensible. Since we have prevented further extensions, isExtensible() now returns false.

The isExtensible() method is useful for checking whether an object can be modified or not. If an object is not extensible, you cannot add new properties to it, but you can still modify or delete its existing properties.