Object.preventExtensions vs Object.seal in JavaScript

In JavaScript, Object.preventExtensions() and Object.seal() are built-in methods used to restrict the mutability of objects. While both methods permanently prevent new properties from being added to an object, they differ in how they handle existing properties. Object.preventExtensions() allows existing properties to be deleted and reconfigured, whereas Object.seal() marks all existing properties as non-configurable, preventing both deletion and property descriptor changes while still allowing writable values to be updated.

Understanding Object.preventExtensions()

Object.preventExtensions() restricts an object so that it can never have new properties added to it. However, it leaves existing properties completely untouched.

Key behaviors of Object.preventExtensions(): * Adding properties: Disallowed. Attempting to add a new property fails silently or throws a TypeError in strict mode. * Deleting properties: Allowed. You can still remove existing properties using the delete operator. * Modifying values: Allowed. If a property is writable, its value can be changed. * Reconfiguring properties: Allowed. Property descriptors (such as enumerable, writable, and configurable) can still be changed.

To check if an object cannot be extended, use Object.isExtensible(obj), which returns false.

const user = { name: "Alice", age: 30 };
Object.preventExtensions(user);

// Adding a new property fails
user.location = "New York"; // Does not add 'location'

// Deleting an existing property works
delete user.age; // true (user is now { name: "Alice" })

// Modifying an existing property works
user.name = "Bob"; // { name: "Bob" }

Understanding Object.seal()

Object.seal() provides a stricter level of immutability. It prevents new properties from being added (making the object non-extensible) and automatically sets the configurable attribute of all existing properties to false.

Key behaviors of Object.seal(): * Adding properties: Disallowed. * Deleting properties: Disallowed. Existing properties cannot be removed. * Modifying values: Allowed, provided the property’s writable attribute is true. * Reconfiguring properties: Disallowed. You cannot change properties from data properties to accessor properties (getters/setters) or modify their descriptors.

To check if an object is sealed, use Object.isSealed(obj), which returns true.

const user = { name: "Alice", age: 30 };
Object.seal(user);

// Adding a new property fails
user.location = "New York"; // Does not add 'location'

// Deleting an existing property fails
delete user.age; // false (user still has 'age')

// Modifying an existing property works
user.name = "Bob"; // { name: "Bob", age: 30 }

Key Differences at a Glance

Action Object.preventExtensions() Object.seal()
Add new properties No No
Delete existing properties Yes No
Modify existing values Yes (if writable) Yes (if writable)
Change property descriptors Yes No
Status check method !Object.isExtensible(obj) Object.isSealed(obj)

When to Use Which

Use Object.preventExtensions() when you want to lock the schema of an object to prevent unexpected keys from being appended, but you still need the flexibility to delete or reconfigure existing attributes.

Use Object.seal() when an object’s structure is finalized and should neither grow nor shrink, ensuring that existing keys remain intact while their values remain editable.