Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

JS Puzzles · Const

Object.freeze

Object.freeze() prevents extensions and makes an object's existing properties non-writable and non-configurable. It returns the same object, while nested objects remain mutable unless frozen separately.

How it works

Object.freeze marks an object as non-extensible and makes its existing data properties non-writable and non-configurable. It returns the same object rather than creating a copy; referenced nested objects remain independently mutable unless they are frozen too.

Syntax

Object.freeze(obj)

Parameters

NameTypeDescription
objobjectThe object to freeze.

Returns

The object that was passed to the function.

Key points

  • The operation first makes the object non-extensible, then tightens descriptors for its existing properties.
  • Existing data-property values cannot be reassigned after freezing.
  • A frozen object's prototype cannot be replaced.
  • Compared with sealing, freezing additionally prevents reassignment of existing data properties.
  • The return value is the exact input reference.

Common mistakes

  • Adding or removing properties, or changing frozen data properties, may fail silently in non-strict code but throws TypeError in strict code.
  • Freezing an accessor property does not freeze the value produced by its getter or prevent its setter from being called.
  • The method returns the original object, so assigning the result to another variable does not produce an independent frozen clone.
  • The operation is shallow: object-valued properties can still be changed unless those referenced objects are frozen separately.

Example: Freeze an object

"use strict";

const settings = Object.freeze({
  mode: "dark",
  nested: { enabled: true },
});

settings.mode = "light"; // TypeError
settings.nested.enabled = false; // Allowed: nested is not frozen

console.log(settings.mode); // "dark"
console.log(settings.nested.enabled); // false

The outer object is frozen, but freezing does not recursively freeze objects referenced by its properties.

When to use it

Protect configuration

Expose configuration objects that callers should not modify.

Stabilize shared data

Prevent accidental mutation of shared constants or state snapshots.

Harden object boundaries

Lock an API object's shape and property descriptors before handing it to other code.

Interview questions

QWhat does Object.freeze do?

Object.freeze prevents extensions and sets existing data properties to non-writable and non-configurable. It also prevents changing the object's prototype. The method returns the same object it receives.

Follow-up: Does it deep-freeze nested objects? No. A nested object must be passed to Object.freeze separately, typically through a deliberate recursive utility.

QWhat happens when code modifies a frozen object?

In strict mode, attempts to add, delete, or assign to restricted properties throw TypeError. In non-strict code, such operations can fail silently.

Follow-up: Would a setter on a frozen object stop working? No. Accessor setters can still be invoked because freezing does not remove accessor behavior.

QDoes Object.freeze create a copy?

Object.freeze returns the input object, not a new copy, so identity is preserved. Freezing also makes the object non-extensible and locks the relevant property descriptors.

Follow-up: How does Object.freeze differ from Object.seal? Sealing prevents extensions and property deletion, while freezing additionally makes existing data properties immutable.

  • Object.seal()
  • Object.preventExtensions()
  • Object.isFrozen()
  • Object.isSealed()
  • Object.defineProperty()

Source: Object.freeze () - JavaScript | MDN. Checked 2026-10-15.