The Object.isExtensible()
method checks if an object is extensible i.e. new properties can be added to it.
Example
// new objects are extensible
let empty = {};
console.log(Object.isExtensible(empty));
// Output: true
isExtensible() syntax
The syntax of the isExtensible()
method is:
Object.isExtensible(obj)
Here, isExtensible()
is a static method. Hence, we need to access the method using the class name, Object
.
isExtensible() Parameters
The isExtensible()
method takes in:
- obj - the object which should be checked.
isExtensible() Return Value
The isExtensible()
method returns a Boolean
value:
true
- if object is extensiblefalse
- if object is not extensible
Note: An object can be marked as non-extensible using Object.preventExtensions()
, Object.seal()
, or Object.freeze()
.
Example: JavaScript Object.isExtensible()
// create an empty object
let obj = {};
// new objects are extensible
console.log(Object.isExtensible(obj));
// Output: true
// prevent extenstion of obj
Object.preventExtensions(obj);
// check if obj is extensible or not
console.log(Object.isExtensible(obj));
// Output: false
In the above example, we have created an empty object obj and checked its extensibility using the isExtensible()
method.
By default, new objects are extensible: we can add new properties to them.
However, we can prevent an object from being extended using the preventExtensions()
method. When we call this method, the object becomes non-extensible.
When we check for the extensibility of obj again, we get false
as an output, indicating that the object is no more extensible.
Note: Sealed and frozen objects are non-extensible by definition.
Recommended Reading: