Programming Search⌘K

Featurecore

Object integrity levels

Also called Object.freeze, Object.seal, Object.preventExtensions, frozen objects, sealed objects, non-extensible objects, deep freeze, Object.isFrozen.

Object.preventExtensions stops new properties, Object.seal also stops deleting and reconfiguring them, and Object.freeze also makes data properties read-only. All three are shallow.

Language
JavaScript

Synopsis

"use strict";
for (const lock of [Object.preventExtensions, Object.seal, Object.freeze]) {
  const o = lock({ x: 1 });
  const changes = {
    add: () => (o.y = 2),
    change: () => (o.x = 3),
    remove: () => delete o.x,
  };
  const result = Object.entries(changes).map(([name, f]) => {
    try {
      f();
      return `${name} ok`;
    } catch {
      return `${name} fails`;
    }
  });
  console.log(lock.name, result.join(", "));
}
Output
preventExtensions add fails, change ok, remove ok
seal add fails, change ok, remove fails
freeze add fails, change fails, remove fails
Level Add Delete or reconfigure Change a data value Test
Object.preventExtensions(o) no yes yes !Object.isExtensible(o)
Object.seal(o) no no yes Object.isSealed(o)
Object.freeze(o) no no no Object.isFrozen(o)

Semantics

r1. Object.preventExtensions "prevents new properties from ever being added to an object (i.e., prevents future extensions to the object). It also prevents the object's prototype from being re-assigned."1 "Note that the properties of a non-extensible object, in general, may still be deleted." "There is no way to make an object extensible again once it has been made non-extensible."1 Example: basics.

r2. "Sealing an object is equivalent to preventing extensions and then changing all existing properties' descriptors to configurable: false . This has the effect of making the set of properties on the object fixed." It "does not prevent the values of data properties from being changed."2 In ECMA-262, SetIntegrityLevel "is used to fix the set of own properties of an object."3

r3. "Freezing an object is equivalent to preventing extensions and then changing all existing properties' descriptors' configurable to false".4 For a data property, SetIntegrityLevel with level frozen uses the descriptor "{ [[Configurable]]: false, [[Writable]]: false }", and for an accessor property it uses "{ [[Configurable]]: false }".3 So a getter on a frozen object can still return a new value each time, and "the setter can still be called without throwing errors when setting the property."4 Example: accessor.

r4. A change that the level forbids "will fail, either silently or by throwing a TypeError exception (most commonly, but not exclusively, when in strict mode)."4 The blocks on this page use strict mode, so the failures throw.

r5. "An object is frozen if and only if it is not extensible, all its properties are non-configurable, and all its data properties (that is, properties which are not accessor properties with getter or setter components) are non-writable."5 TestIntegrityLevel checks extensibility first: "If the object is extensible, none of its properties are examined."3 Object.isSealed returns "? TestIntegrityLevel(O, sealed)" for an object.6 So an empty non-extensible object is both sealed and frozen. Example: tests.

r6. The levels are shallow. "The result of calling Object.freeze(object) only applies to the immediate properties of object itself".4 "Note that values that are objects can still be modified, unless they are also frozen."4 And the prototype is not frozen: "Object.preventExtensions() only prevents addition of own properties. Properties can still be added to the object prototype."1 Examples: proto, shallow.

r7. "freeze() returns the same object that was passed into the function. It does not create a frozen copy."4 Example: copy.

Examples

Testing the levels (rule r5).

const empty = Object.preventExtensions({});
const sealed = Object.seal({ a: 1 });
const frozen = Object.freeze({ a: 1 });
console.log(Object.isFrozen(empty), Object.isSealed(empty));
console.log(Object.isFrozen(sealed), Object.isSealed(sealed), Object.isExtensible(sealed));
console.log(Object.isFrozen(frozen), Object.isSealed(frozen));
Output
true true
false true false
true true

Accessors keep working on a frozen object (rule r3).

"use strict";
let count = 0;
const counter = Object.freeze({
  get next() {
    count += 1;
    return count;
  },
  set start(v) {
    count = v;
  },
});
counter.start = 10;
console.log(counter.next, counter.next, Object.isFrozen(counter));
Output
11 12 true

The prototype stays open, but it cannot be replaced (rules r1 and r6).

const proto = {};
const o = Object.preventExtensions(Object.create(proto));
proto.greet = () => "hi";
console.log(o.greet());
try {
  Object.setPrototypeOf(o, {});
} catch (e) {
  console.log(e.name);
}
Output
hi
TypeError

MDN: "any [[Prototype]] re-assignment will throw a TypeError ."1

Pitfalls

Expecting freeze to freeze nested objects

This code runs but gives a wrong answer on purpose.

"use strict";
const employee = Object.freeze({ name: "Ada", address: { city: "London" } });
employee.address.city = "Paris";
console.log(employee.address.city, Object.isFrozen(employee.address));
function deepFreeze(obj) {
  for (const key of Reflect.ownKeys(obj)) {
    const value = obj[key];
    if (value && typeof value === "object") deepFreeze(value);
  }
  return Object.freeze(obj);
}
const fixed = deepFreeze({ name: "Ada", address: { city: "London" } });
try {
  fixed.address.city = "Paris";
} catch (e) {
  console.log(e.name);
}
console.log(fixed.address.city);
Output
Paris false
TypeError
London

Only the outer object is frozen (rule r6). MDN: "To make an object immutable, recursively freeze each non-primitive property (deep freeze)."4

Expecting const to freeze the object

This code runs but gives a wrong answer on purpose.

const settings = { debug: false };
settings.debug = true;
console.log(settings.debug);
const locked = Object.freeze({ debug: false });
console.log(Reflect.set(locked, "debug", true), locked.debug);
Output
true
false false

"The value of a constant can't be changed through reassignment using the assignment operator, but if a constant is an object, its properties can be added, updated, or removed."7 const fixes the variable, and Object.freeze fixes the object.

Expecting freeze to return a copy

This code runs but gives a wrong answer on purpose.

"use strict";
const original = { n: 1 };
const frozen = Object.freeze(original);
console.log(frozen === original);
try {
  original.n = 2;
} catch (e) {
  console.log(e.name);
}
const copy = { ...frozen };
copy.n = 3;
console.log(original.n, copy.n, Object.isFrozen(copy));
Output
true
TypeError
1 3 false

freeze changes the object you pass it (rule r7). To keep a changeable version, copy it before freezing.

Comparison

Java's final is about the variable, like JavaScript's const: "If a final variable holds a reference to an object, then the state of the object may be changed by operations on the object, but the variable will always refer to the same object."8 JavaScript adds levels on the object itself, so one object can be frozen while every variable that refers to it stays reassignable (rules r1 to r3).

References

  1. MDN Web Docs, JavaScript Reference, Object.preventExtensions(), the introduction; Description
  2. MDN Web Docs, JavaScript Reference, Object.seal(), Description
  3. ECMAScript 2026 Language Specification (ECMA-262, 17th edition), clause 7, 7.3.15 SetIntegrityLevel; 7.3.16 TestIntegrityLevel
  4. MDN Web Docs, JavaScript Reference, Object.freeze(), Description; Deep freezing
  5. MDN Web Docs, JavaScript Reference, Object.isFrozen(), Description
  6. ECMAScript 2026 Language Specification (ECMA-262, 17th edition), clause 20, 20.1.2.16 Object.isExtensible; 20.1.2.18 Object.isSealed
  7. MDN Web Docs, JavaScript Reference, const, the introduction; Description
  8. The Java Language Specification, Java SE 25 Edition, chapter 4, 4.12.4 final Variables

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW3 standard
  • a history with a source