protection

Object.seal

Seals an object: prevents new properties from being added and marks all existing properties as non-configurable. Existing writable properties can still be reassigned.

Object.seal(obj)

Parameters

Parameter Purpose
obj The object to seal
returns The same (now sealed) object

Examples

const o = Object.seal({ a: 1 });
o.a = 2;
console.log(o.a);

Logs 2 — existing values are still writable

const o = Object.seal({ a: 1 });
try { o.b = 2; } catch (e) {}
console.log(o.b);

Logs undefined — adding new properties fails

const o = Object.seal({ a: 1 });
try { delete o.a; } catch (e) {}
console.log(o.a);

Logs 1 — deletions fail because properties are non-configurable

console.log(Object.isSealed(Object.seal({})));

Logs true

Gotcha

Weaker than freeze — sealed objects can still have their existing writable values reassigned. Like freeze, it is shallow and does not affect nested objects.

Related methods

← All JS Object methods · Array methods · Spread vs rest