Object.freeze vs Object.seal vs const in JavaScript

IntermediateJavaScript interview questionBy , updated 17 November 2026

Short answer

const protects the variable, not the value: you cannot reassign it, but you can still change the object it points to. Object.seal protects the object's shape: no properties added or deleted, but existing ones can change. Object.freeze protects the shape and the values: nothing can be added, deleted or changed. Seal and freeze are shallow, so nested objects stay mutable.

The comparison

Add a property Delete a property Change a value Reassign the variable
const obj = {} Yes Yes Yes No
Object.preventExtensions(obj) No Yes Yes Depends on let/const
Object.seal(obj) No No Yes Depends on let/const
Object.freeze(obj) No No No Depends on let/const

The last column shows the key idea: const and the Object methods work on different things. const is about the binding (the name). The three methods are about the object. They combine freely: const config = Object.freeze({...}) gives you a name that cannot be reassigned pointing to an object that cannot be changed.

const: the binding is fixed

const user = { name: "Asha" };
user.name = "Ravi"; // fine
user.age = 30; // fine
user = {}; // TypeError: Assignment to constant variable.

For primitives such as strings and numbers, const does make the value fixed, because primitives cannot be changed anyway. For objects and arrays it only stops you pointing the name somewhere else. The rest of const's rules, such as block scope, are in var vs let vs const.

What the methods actually change

Every property has flags: writable (can the value change) and configurable (can it be deleted or redefined). Every object has an internal "extensible" flag (can new properties be added). The three methods are shortcuts for setting those flags.

  • Object.preventExtensions(obj) makes the object non-extensible. Nothing else changes.
  • Object.seal(obj) makes it non-extensible and marks every own property configurable: false.
  • Object.freeze(obj) does what seal does and also marks every data property writable: false.

Each has a check: Object.isExtensible, Object.isSealed and Object.isFrozen. All three are one-way. There is no unfreeze; make a copy instead.

const settings = Object.seal({ theme: "dark" });
settings.theme = "light"; // allowed: sealed values can change
settings.size = "lg"; // ignored (or TypeError in strict mode)
delete settings.theme; // false (or TypeError in strict mode)

const point = Object.freeze({ x: 1, y: 2 });
point.x = 99; // ignored (or TypeError in strict mode)
point.x; // 1

Freeze does not stop accessor properties from working. A setter on a frozen object still runs; it just cannot change the frozen data properties directly. Property flags are explored in Predict getter, setter and property descriptor results.

Silent failure vs strict-mode errors

What happens when you break the rules depends on the mode.

  • Sloppy mode: the write, add or delete is ignored with no error. Your code carries on with the old value, which makes bugs hard to find.
  • Strict mode: it throws a TypeError, such as "Cannot assign to read only property 'x' of object" or "Cannot add property size, object is not extensible".

ES modules and class bodies are always strict, so in most modern code you get the error. Built-in methods throw in every mode: Object.freeze(arr).push(1) throws a TypeError even in a sloppy script. See strict mode for the other changes.

Freeze is shallow

const config = Object.freeze({ api: { url: "/v1" }, flags: ["beta"] });

config.api = {}; // blocked
config.api.url = "/v2"; // works: api is a separate, unfrozen object
config.flags.push("new"); // works: the array is not frozen

Only the top-level properties are locked. To lock everything, freeze each nested object too:

function deepFreeze(value, seen = new WeakSet()) {
  if (value === null || (typeof value !== "object" && typeof value !== "function")) return value;
  if (seen.has(value)) return value; // handles circular references
  seen.add(value);

  for (const key of Reflect.ownKeys(value)) {
    const descriptor = Object.getOwnPropertyDescriptor(value, key);
    if ("value" in descriptor) deepFreeze(descriptor.value, seen); // skip getters
  }
  return Object.freeze(value);
}

Reflect.ownKeys includes symbol and non-enumerable keys, which Object.keys would miss. Reading descriptors avoids running getters. The WeakSet stops infinite recursion on cycles, and is safer than skipping objects that are already frozen, because a frozen parent can still hold unfrozen children.

Deep freeze has limits. A frozen Map or Set can still be changed with set, add and delete, because their entries are not properties. Freezing a typed array that has elements throws a TypeError. Freezing does not freeze the prototype either.

When to use each

  • const: by default for every variable you do not reassign.
  • Object.freeze: constants and config objects shared across modules, enum-like lookup objects, and test fixtures you want to protect from accidental mutation. Immer, for example, freezes the state it produces by default so that accidental mutation fails loudly.
  • Object.seal: objects with a fixed set of fields whose values change, where a typo like user.nmae = "x" should fail instead of adding a new property.
  • Object.preventExtensions: rare. It suits library code that wants to stop new properties but still allow deletes.

In TypeScript, Object.freeze returns a Readonly<T> type, which is also shallow. Type-only alternatives like as const do nothing at runtime.

Practise it

Common follow-up questions

Does Object.freeze make a copy?

No. It changes the object in place and returns the same object. Every reference to it sees the frozen version. To keep an editable original, copy first; shallow vs deep copy covers the options.

Can you unfreeze an object?

No. Freezing cannot be undone. Create a new object with spread or structuredClone, which gives you an unfrozen copy.

Is a frozen object faster?

Do not freeze for speed. Engines may optimise some frozen objects, but the gain is not reliable. Freeze to prevent bugs, not to make code faster.

What is the difference between sealed and frozen in one sentence?

A sealed object can change its existing values; a frozen object cannot change anything.

Mistakes to avoid

  • Believing const makes an object immutable

  • Assuming Object.freeze reaches nested objects and arrays

  • Testing in a sloppy script, seeing no error, and concluding the write worked

  • Freezing a Map or Set and expecting its contents to be locked

  • Writing a deep freeze with Object.keys and no cycle check

Practise it in code

Interviewers rarely stop at the definition. These questions open in an editor with tests.

  1. Predict getter, setter and property descriptor results: defineProperty, freeze and sealEasy25 min
  2. Implement deepFreeze() and isDeepFrozen() that handle cycles, symbols and already-frozen objectsEasy15 min
  3. Predict quick-fire results: a detached method, a shadowed let, a shallow Object.freeze, Promise.resolve(fn) and -0Easy20 min
  4. Predict strict mode versus sloppy mode results: implicit globals, this, frozen objects and argumentsEasy25 min
  5. Predict property-descriptor results: getters and setters through spread, Object.assign, JSON.stringify, structuredClone and Object.freezeEasy25 min