Object.preventExtensions(obj) marks an object so you cannot add new own properties. Existing properties can still be changed or deleted (unless you sealed or froze them).
It returns the same object. Check with Object.isExtensible(obj). This is the weakest of the three locks: preventExtensions < seal < freeze.
Syntax
Call Object.preventExtensions() like this. Start with this line.
JavaScript
Object.preventExtensions(obj)
Call
What it does
Object.preventExtensions(obj)
Makes obj non-extensible and returns it.
Object.isExtensible(obj)
Returns false after preventExtensions.
obj.newKey = 1
Fails quietly in non-strict mode; throws in strict mode.
Pass the object to lock against new properties. The call returns that same object.
Library declarations
JavaScript
Object.preventExtensions(obj)
JavaScript
Object.preventExtensions<T>(o: T): T
Parameters
Parameter
Description
obj
Required. The object that should no longer accept new own properties.
Return Value
Return Value
Description
object
The same object you passed in, now non-extensible.
JavaScript Examples
Stop Adding Properties
Prevent extensions, then try to add a new key.
JavaScript
var car = { brand: "Tata" };
Object.preventExtensions(car);
console.log(Object.isExtensible(car));
car.brand = "Mahindra";
car.year = 2024;
console.log(car.brand);
console.log(car.year);