JavaScript Object.seal()

Definition and usage

Object.seal() This method is used to prevent the addition or deletion of object properties, and makes existing properties non-configurable.

Object.seal() This method will silently fail in non-strict mode.

Object.seal() This method will throw a TypeError in strict mode.

Can be used Object.isSealed() Method to check if an object is sealed.

Related methods:

Object.preventExtensions() Allows modification, but prevents addition of properties.

Object.seal() Allows modification, but prevents addition and deletion of properties.

Object.freeze() Prevents modification, addition, and deletion of properties.

Object.isExtensible() Returns true if the object is extensible.

Object.isSealed() Returns true if the object is sealed.

Object.isFrozen() Returns true if the object is frozen.

Instance

Example 1

"use strict"
// Create an object
const person = {
  firstName: "Bill",
  lastName: "Gates",
  age: 50,
  eyeColor: "blue"
};
// Seal the object
Object.seal(person);
// The following operation will throw an error
delete person.age;

Try It Yourself

Example 2

const fruits = ["Banana", "Orange", "Apple", "Mango"];
Object.seal(fruits);
// The following operation will throw an error:
fruits.push("Kiwi");

Try It Yourself

Syntax

Object.seal(object)

Parameter

Parameter Description
object Required. The object to be sealed.

Return Value

Type Description
Object The object sealed.

Browser Support

Object.seal() Is a feature of ECMAScript5 (ES5).

Since July 2013, all modern browsers have fully supported ES5 (JavaScript 2009):

Chrome Edge Firefox Safari Opera
Chrome 23 IE/Edge 11 Firefox 21 Safari 6 Opera 15
2012.9 2012.9 2013.4 2012.7 2013.7