Open In App

What is Object.seal() method in JavaScript ?

Last Updated : 07 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The Object.seal() method in JavaScript is used to seal an object, preventing new properties from being added to it and existing properties from being removed. However, the values of existing properties can still be changed.

Syntax:

Object.seal(obj)

Example: Here, Object.seal() is used to seal the obj object. Attempts to add or remove properties have no effect, but modifying existing properties (prop) is allowed. The object’s structure remains unchanged, demonstrating the behavior enforced by Object.seal().

Javascript




const obj = { prop: 42 };
Object.seal(obj);
 
// Attempting to add a new property
obj.newProp = 10;
 
// Attempting to delete an existing property
delete obj.prop;
 
// Modifying an existing property
obj.prop = 33;
 
console.log(obj); // Output: { prop: 33 }


Output

{ prop: 33 }

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads