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

JavaScript · Function

Object.freeze

Object.freeze() prevents extensions and locks an object's existing properties. It returns the same object reference and does not recursively freeze nested objects.

How it works

Object.freeze() makes an object non-extensible and locks its existing own property descriptors, returning the same object reference. It does not recursively freeze objects stored in those properties.

Syntax

Object.freeze(obj)

Parameters

NameTypeDescription
objobjectThe object to freeze.

Returns

The object that was passed to the function.

Key points

  • It blocks new properties and locks existing data properties against reassignment and reconfiguration.
  • A frozen object cannot have its own properties added, removed, or structurally modified.
  • Freezing also prevents replacing the object's prototype.
  • Existing data-property values remain fixed after freezing.
  • The return value is the identical reference supplied as the argument.

Common mistakes

  • Freezing is shallow: objects held in properties are not frozen automatically.
  • Adding, removing, or changing properties can either fail silently or throw, depending on strict mode.
  • A getter may still produce changing values, and a setter may still run even though the object is frozen.
  • The function returns the original object rather than producing a separate frozen object.

Example: Freeze an object

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

console.log(settings.theme); // "dark"
try {
  settings.theme = "light";
} catch (error) {
  console.log(error instanceof TypeError); // true
}

settings.nested.enabled = false; // Nested object is not frozen
console.log(settings.nested.enabled); // false

The outer object rejects direct property changes, while its nested object remains mutable because freezing is shallow.

When to use it

Immutable configuration

Protect configuration objects from accidental direct edits after initialization.

Constant-like records

Expose a stable set of constants whose properties should not be reassigned.

Runtime mutation checks

Enforce read-only object properties at runtime in strict-mode code.

Interview questions

QWhat does Object.freeze() do?

Object.freeze() prevents extensions and makes existing own properties non-configurable; data properties also become non-writable. It returns the object passed to it, so the operation changes that object in place.

Follow-up: How would you freeze nested data? Recursively call Object.freeze() on nested objects, because the built-in operation only affects immediate properties.

QWhich operations does a frozen object prevent?

No new properties can be added, existing properties cannot be removed, and data-property values cannot be reassigned. The object's prototype also cannot be reassigned.

Follow-up: What happens when code attempts a forbidden operation? It may be ignored in non-strict code or raise TypeError, especially in strict mode.

QIs Object.freeze() deep?

Object.freeze() is shallow. If a frozen object's property contains another object, that nested object can still be changed unless it is frozen separately.

Follow-up: Does freezing an accessor property freeze the value returned by its getter? No; the getter and setter retain their accessor behavior, so the returned value may still vary.

  • Object.preventExtensions()
  • Object.seal()
  • Object.isFrozen()
  • Object.getOwnPropertyDescriptors()

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