Object.freeze()

Freezes an object, preventing new properties and changes to existing ones.

Since ES5 Spec ↗

Syntax

Object.freeze(obj)

Parameters

NameTypeRequiredDescription
obj object Yes The object to freeze.

Returns

object — The same object, now frozen.

Examples

const o = Object.freeze({ a: 1 });
o.a = 2;
console.log(o.a);
Output
1
const o = Object.freeze({ nested: { x: 1 } });
o.nested.x = 9;
console.log(o.nested.x);
Output
9

Notes

A shallow freeze - nested objects can still be mutated. Mutating a frozen object fails silently in sloppy mode and throws in strict mode. Use `Object.isFrozen` to test.

Browser & runtime support

EnvironmentSince version
chrome 6
firefox 4
safari 5.1
edge 12
node 0.10

See also