In JavaScript, the isExtensible() method is used to determine if new properties can be added to an object. This method checks the extensibility of an object, which means if it can be modified to add new properties or not.
By default, any object in JavaScript is extensible unless it has been used as an argument to certain methods like Object.freeze(), Object.seal(), or Object.preventExtensions(). These methods restrict the modification capabilities of an object.
Here are some examples to illustrate the usage of the isExtensible() method:
const dog = {}
Object.isExtensible(dog) // true
In the above example, a new object “dog” is created and isExtensible() is used to check if properties can be added. In this case, the method returns true, indicating that the object is extensible and new properties can be added.
const cat = {}
Object.freeze(cat)
Object.isExtensible(cat) // false
In this second example, the object “cat” is created and then frozen using Object.freeze(). The isExtensible() method is called on the “cat” object, and it returns false. This indicates that the object is not extensible and new properties cannot be added.
In conclusion, the isExtensible() method is a useful tool to check if an object can be modified by adding new properties. It helps in controlling the extensibility of objects and provides flexibility in managing object properties.