CKTIS / JavaScript
Object.freeze: top-level non-extension and non-writability
Object.freeze prevents extension and reconfiguration of an object’s own properties and makes its own data properties non-writable.
Version: ECMAScript 5 onward; primitive-return behavior since ECMAScript 2015; example target: Node.js 24.22.2
Syntax
Object.freeze(value)Object.freeze(value) returns a primitive unchanged. For an object it performs SetIntegrityLevel with the frozen level: it prevents new properties, sets configurable: false on every own property, and also sets writable: false on own data properties. It returns the same object.
An accessor property retains its getter and setter; only its configurability is removed. Freezing is shallow: nested objects, private elements, and the internal data of Map and Set do not automatically become immutable. A setter can still mutate nested or external state. Deep immutability requires a controlled, cycle-safe traversal of the data graph.
The method has existed since ECMAScript 5; returning non-object values instead of throwing applies since ECMAScript 2015. Failure of PreventExtensions or a Proxy invariant violation throws TypeError. Typed arrays with elements cannot be frozen and also throw TypeError, whereas a zero-length typed array can be frozen.
Examples
const config = {
nested: { enabled: false },
get status() { return this.nested.enabled; },
set status(value) { this.nested.enabled = value; }
};
const same = Object.freeze(config);
const nested = Object.getOwnPropertyDescriptor(config, "nested");
const status = Object.getOwnPropertyDescriptor(config, "status");
console.log(same === config, Object.isFrozen(config));
console.log(nested.writable, nested.configurable, typeof status.set, status.configurable);
config.status = true;
console.log(config.nested.enabled, config.status);
console.log(Object.freeze(7));
try { Object.freeze(new Uint8Array([1])); }
catch (error) { console.log(error.name); }Result
true true false false function false true true 7 TypeError
The example confirms data and accessor attributes, shallow freezing, primitive handling, and the typed-array error.