Object.seal() method - JavaScript Tutorial

Object.seal() method

BLUF: This tutorial on Object.seal() method provides an in-depth look at JavaScript's core features. It includes practical examples and code snippets to help you master modern JS development.
Key Discovery: Object.seal() method

Understanding Object.seal() method is crucial for building dynamic, interactive web applications. Explore the examples below to see it in action.

The Object.seal function in JavaScript is utilized to seal an object, thereby disallowing the addition of new properties and designating all current properties as non-configurable. The object intended for sealing is provided as a parameter, and the method subsequently returns the sealed object.

Syntax:

Example

Object.seal(obj)

Parameter:

obj : It is the object which should be sealed.

Return value:

The Object.sealed function returns the object that has been sealed.

Browser Support:

Chrome 6
Edge Yes
Firefox 4
Opera 12

Example 1

Example

const obj1 = { property1: 'Marry'};
        const obj2 = Object.seal(obj1);
       // prevents other code from deleting properties of an object.
        obj2.property1 = 'carry';
        console.log(obj2.property1);

Output:

Output

"carry"

Example 2

Example

const object1 = {
  property1: 29
};
Object.seal(object1);
// Prevents other code from deleting properties of an object.
object1.property1 =45;
console.log(object1.property1);
delete object1.property1;
   // cannot delete when sealed

Output:

Example 3

Example

const object1 = {
  property1: 42
};
Object.seal(object1);
object1.property1 = 45;
console.log(object1.property1);

delete object1.property1; // cannot delete when sealed
console.log(object1.property1);

const object2 = {
  property2: 45};
object2.property2 =67;
console.log(object2.property2);

Output:

Output

45
45
67

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience