What does Object.freeze do?
By FrontendPro Editorial Team Updated 9/26/2026
Answer
It makes an object's own properties read-only and prevents adding or deleting any — a one-way operation that cannot be undone.
const config = Object.freeze({ api: "/v1", retries: 3 });
config.api = "/v2"; // ignored silently (TypeError in strict mode)
config.timeout = 1000; // ignored
delete config.retries; // ignored
Object.isFrozen(config); // true
Note the silence. In sloppy mode the assignment simply does nothing, which is confusing during debugging; in strict mode and modules it throws, which is better.
It is shallow
This is the detail that catches everyone:
const settings = Object.freeze({ theme: { dark: true } });
settings.theme.dark = false; // works — the nested object is not frozen
A deep freeze means recursing yourself:
function deepFreeze(obj) {
for (const v of Object.values(obj)) {
if (v && typeof v === "object") deepFreeze(v);
}
return Object.freeze(obj);
}
The related methods
| Add | Delete | Modify existing | |
|---|---|---|---|
preventExtensions | No | Yes | Yes |
seal | No | No | Yes |
freeze | No | No | No |
Where it is genuinely useful
Configuration objects and constant lookup tables, where an accidental write is a real bug. Enforcing immutability in a shared module so no consumer can mutate exported state.
Where it is not
It is not a substitute for immutable update patterns. In React you do not freeze state — you create new objects with spread, and the benefit comes from the changed reference, which freezing does not give you.
There is also a small runtime cost, and it can deoptimise hot paths in some engines, so freezing large objects on every render is a bad trade. Freeze at module setup, not in a loop.
