JavaScript Reference ยท Object
Object.isFrozen()
Reference for JavaScript Object.isFrozen(): test whether an object is frozen, including empty non-extensible objects and primitives, with examples.
Object.isFrozen() returns true if an object is frozen: it cannot gain properties, and every existing property is non-configurable and, for data properties, non-writable. It does not matter how the object got into this state. Object.freeze() is the usual way, but careful use of preventExtensions() and defineProperty() produces the same result.
Primitive values (numbers, strings, null, ...) are always reported as frozen, because they cannot be changed.
Object.isFrozen(Object.freeze({ a: 1 })); // true
Try it
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Syntax
Object.isFrozen(obj)
| Name | Description |
|---|---|
obj |
The value to test. |
Return value: true if obj is frozen (or is not an object), otherwise false.
Exceptions: none.
Usage notes
- Three conditions must all hold: the object is not extensible, no property is configurable, and no data property is writable. Accessor properties only need to be non-configurable.
- Empty and non-extensible means frozen. An object with no properties that has been passed through
Object.preventExtensions()is frozen, because there is nothing left to change. - Primitives are frozen.
Object.isFrozen(5)istrue. Older engines threw aTypeErrorinstead, so do not rely on this in very old code. - Shallow check. An object can be frozen while objects inside it are not. Check nested values yourself.
- Related checks:
Object.isSealed()andObject.isExtensible()test the weaker states. Every frozen object is also sealed and non-extensible. - Use cases: guarding functions that expect immutable input, tests, and the base case of a recursive
deepFreeze(). - Not a security boundary. Frozen objects can still be replaced where they are referenced, and accessors can still hold mutable state.
Browser support
Object.isFrozen() is available in every browser and JavaScript runtime.