What is Object.seal() method in JavaScript ?
Last Updated :
07 Feb, 2024
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);
obj.newProp = 10;
delete obj.prop;
obj.prop = 33;
console.log(obj);
|
Share your thoughts in the comments
Please Login to comment...