JavaScript .preventExtensions()

jjw's avatar
Published Aug 24, 2025
Contribute to Docs

The Object.preventExtensions() method prevents new properties from being added to an object. Unlike Object.freeze() or Object.seal(), this method still allows existing properties to be modified or deleted. It returns the same object that was passed to it.

Note: Once an object is made non-extensible, it cannot be made extensible again. This operation is irreversible.

  • Front-end engineers work closely with designers to make websites beautiful, functional, and fast.
    • Includes 34 Courses
    • With Professional Certification
    • Beginner Friendly.
      115 hours
  • Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.
    • Beginner Friendly.
      15 hours

Syntax

Object.preventExtensions(obj)

Parameters:

  • obj: The object to make non-extensible.

Return value:

Returns the same object that was passed in, now marked as non-extensible.

Example: Basic Usage of Object.preventExtensions()

The following code demonstrates how Object.preventExtensions() works:

const user = {
name: 'Arthur',
age: 42,
};
// Show object is extensible
console.log(Object.isExtensible(user));
// Prevent extensions
Object.preventExtensions(user);
// Show object is no longer extensible after using Object.preventExtensions()
console.log(Object.isExtensible(user));
// New properties cannot be added
user.email = '[email protected]';
console.log(user.email);
// Existing properties can be modified
user.age = 31;
console.log(user.age);
// Existing properties can be deleted
delete user.name;
console.log(user.name);

The output generated by this code is:

true
false
undefined
31
undefined

Codebyte Example

This example shows the effect of Object.preventExtensions() in non-strict mode, and notes how strict mode would throw a TypeError:

Code
Output

All contributors

Contribute to Docs

Learn JavaScript on Codecademy

  • Front-end engineers work closely with designers to make websites beautiful, functional, and fast.
    • Includes 34 Courses
    • With Professional Certification
    • Beginner Friendly.
      115 hours
  • Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.
    • Beginner Friendly.
      15 hours