Quiz

What is `Object.preventExtensions()` for?

Topics
JavaScript

TL;DR

Object.preventExtensions(object) makes that object non-extensible: new own properties cannot be added and its prototype cannot be changed, while existing properties may still be changed or deleted according to their descriptors. The operation is shallow and irreversible for that object. In strict mode, invalid additions throw; otherwise ordinary assignment may fail silently.

const obj = { name: 'John' };
Object.preventExtensions(obj);
obj.age = 30; // This will not work, as the object is not extensible
console.log(obj.age); // undefined

What is Object.preventExtensions() for?

Object.preventExtensions() is a method in JavaScript that is used to prevent new properties from being added to an object. This method is part of the ECMAScript 5 specification and is useful for maintaining the integrity of an object by ensuring that its structure cannot be altered by adding new properties.

Syntax

Object.preventExtensions(obj);
  • obj: The object which should be made non-extensible.

Behavior

  • Once an object is made non-extensible, you cannot add new properties to it.
  • Existing properties can still be modified or deleted.
  • The method returns the object that was passed to it.

Example

const obj = { name: 'John' };
Object.preventExtensions(obj);
obj.age = 30; // This will not work, as the object is not extensible
console.log(obj.age); // undefined
obj.name = 'Jane'; // This will work, as existing properties can be modified
console.log(obj.name); // 'Jane'
delete obj.name; // This will work, as existing properties can be deleted
console.log(obj.name); // undefined

Checking if an object is extensible

You can check if an object is extensible using the Object.isExtensible() method.

const obj = { name: 'John' };
console.log(Object.isExtensible(obj)); // true
Object.preventExtensions(obj);
console.log(Object.isExtensible(obj)); // false

Comparison with Object.seal() and Object.freeze()

  • Object.preventExtensions(): Prevents adding new properties. Existing properties can still be modified, configured, or deleted.
  • Object.seal(): Prevents adding new properties and marks existing properties as non-configurable, so they cannot be deleted. Their values can still be changed.
  • Object.freeze(): Prevents adding new properties, prevents deleting existing properties, and makes existing properties read-only (a shallow freeze).

Use cases

  • Immutable object structure: When you want to ensure that the structure of an object remains unchanged, you can use Object.preventExtensions().
  • Defensive API design: It can catch accidental shape changes in strict-mode code. It is not a security boundary: existing writable values and nested objects remain mutable, and code may keep references to other sensitive state.

Further reading

Exercises

Check your understanding
Beta
Check your understanding Exercise
Check your understanding Exercise

What does this non-strict code log?

const object = { count: 1, removable: true };
Object.preventExtensions(object);
object.count = 2;
object.extra = 3;
delete object.removable;
console.log(object.count, object.extra, 'removable' in object);