Object.preventExtensions() JavaScript

The Javascript Object preventExtensions() method prevents any extensions of an object i.e. no new property can be added and no existing property can be modified.

Note: Prevention on an object will be permanent and the object cannot be extensible again after applying prevention.

Syntax:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Object.preventExtensions(object)
Object.preventExtensions(object)
Object.preventExtensions(object)

Parameters:
object: It represents the object that has to be prevented from any extensions.

Return:
Non extensible object.

Example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<!DOCTYPE html>
<html>
<body>
<script>
const a = {};
Object.preventExtensions(a);
a.alpha = 56;
document.write(a.hasOwnProperty("alpha"));
</script>
</body>
</html>
<!DOCTYPE html> <html> <body> <script> const a = {}; Object.preventExtensions(a); a.alpha = 56; document.write(a.hasOwnProperty("alpha")); </script> </body> </html>
<!DOCTYPE html>
<html>
<body>
<script>
const a = {};
Object.preventExtensions(a);
a.alpha = 56;
document.write(a.hasOwnProperty("alpha"));
</script>
</body>
</html>