Skip to content

[Term Entry] JavaScript Objects: preventExtensions() #7411

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
---
Title: '.preventExtensions()'
Description: 'Prevents new properties from being added to an object while allowing existing properties to be modified or deleted.'
Subjects:
- 'Code Foundations'
- 'Computer Science'
- 'Web Development'
Tags:
- 'Objects'
- 'Properties'
- 'Methods'
CatalogContent:
- 'introduction-to-javascript'
- 'paths/front-end-engineer-career-path'
---

The **`Object.preventExtensions()`** method prevents new properties from being added to an object. Unlike [`Object.freeze()`](https://www.codecademy.com/resources/docs/javascript/objects/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.

## Syntax

```pseudo
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:

```js
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:

```shell
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`:

```codebyte/javascript
const config = {
theme: 'dark',
language: 'en'
};

console.log('Before preventExtensions:', Object.isExtensible(config));

Object.preventExtensions(config);

console.log('After preventExtensions:', Object.isExtensible(config));

// Non-strict mode: fails silently
config.debug = true;
console.log('Attempted to add debug property:', config.debug); // undefined

// Strict mode: will throw TypeError if uncommented
// 'use strict';
// config.debug = true;

// Modifying existing properties works
config.theme = 'light';
console.log('Modified theme:', config.theme); // light

// Deleting existing properties works
delete config.language;
console.log('After deleting language:', config); // { theme: 'light' }
```