JavaScript .seal()
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(), 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 modewhen working with sealed objects, as it helps detect unauthorized modifications and prevents silent errors.
Syntax
Object.seal(obj)
Parameters:
obj: The object to seal.
Return value:
The Object.seal() method returns 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:
const person = {name: 'Joe',age: 30,};// Sealing the person objectObject.seal(person);// Modifying the age property will workperson.age = 31;// Attempting to add or delete a property will not workperson.city = 'Madrid';delete person.name;console.log(person);
The code will produce this output:
{ name: 'Joe', age: 31 }
Example 2: Check Sealed Object
The following example uses the Object.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():
const car = {brand: 'Audi',model: 'Sedan',};// Sealing the car objectObject.seal(car);// This will print 'true' because the car object is sealedconsole.log(Object.isSealed(car));car.brand = 'Honda';car.color = 'red';console.log(car);
The code will produce this output:
true{ brand: 'Honda', model: 'Sedan' }
Codebyte Example: Strict Mode Behavior
This example demonstrates the core functionality of Object.seal() by showing what operations are allowed versus prohibited on a sealed object. The sealed object allows modification of existing properties but prevents structural changes like adding new properties or deleting existing ones:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve 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