preventExtensions() method - JavaScript Tutorial

preventExtensions() method

BLUF: This tutorial on preventExtensions() method provides an in-depth look at JavaScript's core features. It includes practical examples and code snippets to help you master modern JS development.
Key Discovery: preventExtensions() method

Understanding preventExtensions() method is crucial for building dynamic, interactive web applications. Explore the examples below to see it in action.

Using Object.preventExtensions ensures that no new properties can be added to an object, effectively blocking any future extensions to the object. This action is irreversible, as once an object is rendered non-extensible, it cannot be reverted back to an extensible state.

Syntax:

Example

Object.preventExtensions(obj)

Parameter:

obj: This is the object that needs to be made non-extensible.

Return value:

It returns the object being made non-extensible.

Browser Support:

Chrome 6
Edge Yes
Firefox 4
Opera 12

Example 1

Example

const uu = {};
Object.preventExtensions(uu);
console.log(
    Object.isExtensible(uu)
);

Output:

Example 2

Example

const obj = {};
Object.preventExtensions(obj);
obj.o = 3;

console.log(
    obj.hasOwnProperty("o")
);

Output:

Example 3

Example

const t = {"p":3};
Object.preventExtensions(t);
delete t.p;
console.log ( t.hasOwnProperty ( "p" ) );
//expected output: false

Output:

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience