JavaScript Object.preventExtensions() Method
The Object.preventExtensions() method in JavaScript is standard built-in objects which prevents new properties from ever being added to an object.
Syntax:
Object.preventExtensions( obj )
Parameters: This method accepts single parameter as mentioned above and described below:
Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!
- obj: This parameter holds the object which must be made non-extensible.
Return value: This method returns the object after making it non-extensible.
Below examples illustrate the Object.preventExtensions() method in JavaScript:
Example 1:
javascript
let geeks1 = {}; Object.preventExtensions(geeks1); try { Object.defineProperty(geeks1, 'prop1', { value: "GFG", property1:"Geeksforgeeks", property2:"Best platform to learn" });} catch (error) { console.log(error);} |
Output:
TypeError: Cannot define property prop1, object is not extensible
Example 2:
javascript
var geeks = {};var geeks0 = Object.preventExtensions(geeks);console.log( geeks0 === geeks); const geeks1 = {"prop1": 555}; Object.preventExtensions(geeks1); delete geeks1.prop1; console.log ( geeks1.hasOwnProperty ( "prop1" ) ); const geeks2 = {}; Object.preventExtensions(geeks2); geeks2.prop2 = 3; console.log( geeks2.hasOwnProperty("prop2") ); const geeks3 = {}; Object.preventExtensions(geeks3); console.log( Object.isExtensible(geeks3) ); |
Output:
true false false false
Supported Browsers: The browsers supported by Object.preventExtensions() method are listed below:
- Chrome 6 and above
- Edge 12 and above
- Firefox 4 and above
- Internet Explorer 9 and above
- Opera 12 and above
- safari 5.1 and above


