JavaScript (ES2026) reference · Object Methods

JavaScript Object.freeze() Method

Make an object immutable: no properties can be added, removed or changed. The freeze is shallow.

JavaScript (ES2026) Since ES5

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

Object.freeze(obj)

Parameters

ParameterDescription
obj The object to inspect or change.

Return value

The same object, now frozen.

Examples

Constants that can't changeTry it yourself
"use strict";
const ROLES = Object.freeze({ ADMIN: "admin", USER: "user" });
try { ROLES.ADMIN = "root"; } catch (e) { console.log(e.constructor.name); }
console.log(ROLES.ADMIN, Object.isFrozen(ROLES));

Output

TypeError
admin true
Freezing is shallowTry it yourself
const cfg = Object.freeze({ db: { host: "localhost" } });
cfg.db.host = "changed"; // nested objects aren't frozen
console.log(cfg.db.host);

Output

changed

Version history

VersionChange
JavaScript ES5Added

Was this page helpful?