Skip to content

[Term Entry] JavaScript Objects: seal() #7475

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 3 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
120 changes: 120 additions & 0 deletions content/javascript/concepts/objects/terms/seal/seal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
---
Title: '.seal()'
Description: 'Prevents new properties from being added to an object and marks all existing properties as non-configurable'
Subjects:
- 'Computer Science'
- 'Web Development'
Tags:
- 'Methods'
- 'Objects'
- 'Properties'
CatalogContent:
- 'introduction-to-javascript'
- 'paths/front-end-engineer-career-path'
---

The **`Object.seal()`** static method seals an object, preventing new properties from being added and making all existing properties non-configurable. In other words:

- New properties cannot be added.
- Existing properties cannot be removed.
- Property enumerability and configurability cannot be changed.

Unlike [`Object.freeze()`](https://www.codecademy.com/resources/docs/javascript/objects/freeze), objects sealed with `Object.seal()` may still have their existing properties updated if they are writable (`writable: true`).

> **Note:** For debugging purposes, it is good practice to use [`strict mode`](https://www.codecademy.com/resources/docs/javascript/strict-mode) when working with sealed objects, as it helps detect unauthorized modifications and prevents silent errors.

## Syntax

```pseudo
Object.seal(obj)
```

**Parameters:**

`obj`: The object to seal.

**Return value:**

The `Object.seal()` method returns the the sealed object.

## Example 1: Basic Object Sealing

In this example, the `person` object is sealed. In non-strict mode, attempts to add or remove properties will fail silently (no error is thrown), while modifying writable properties will still work:

```js
const person = {
name: 'Joe',
age: 30,
};

// Sealing the person object
Object.seal(person);

// Modifying the age property will work
person.age = 31;

// Attempting to add or delete a property will not work
person.city = 'Madrid';
delete person.name;

console.log(person);
```

The code will produce this output:

```shell
{ name: 'Joe', age: 31 }
```

## Example 2: Check Sealed Object

The following example uses the [`Object.isSealed()`](https://www.codecademy.com/resources/docs/javascript/objects/isSealed) to check if the `car` object is sealed. This method returns `true` if the object is sealed, regardless of how it was sealed (e.g., via `Object.seal()` or [`Object.freeze()`](https://www.codecademy.com/resources/docs/javascript/objects/freeze)):

```js
const car = {
brand: 'Audi',
model: 'Sedan',
};

// Sealing the car object
Object.seal(car);

// This will print 'true' because the car object is sealed
console.log(Object.isSealed(car));

car.brand = 'Honda';
car.color = 'red';

console.log(car);
```

The code will produce this output:

```shell
true
{ brand: 'Honda', model: 'Sedan' }
```

## Codebyte Example: Strict Mode Behavior

This example enables strict mode with `'use strict'` so that invalid operations on a sealed object throw errors. Specifically, deleting a property from a sealed object will throw a `TypeError` instead of failing silently:

```codebyte/javascript
'use strict';

const obj = {
property: 'initial-data'
};

obj.property = 'new-data';

console.log(obj.property);

Object.seal(obj);

try {
delete obj.property;
} catch (error) {
console.log(error);
}
```