JavaScript Object.freeze() Method

Definition
Object.freeze(obj) locks an object. You cannot add properties, delete properties, or change existing values on that object.
The freeze is shallow: nested objects inside are not frozen unless you freeze them too. The method returns the same object.
Syntax
Call Object.freeze() like this. Start with this line.
| Call | What it does |
|---|
Object.freeze(obj) | Freezes obj in place and returns the same object. |
Built into JavaScript. Call on Object — no import required.
Library declarations
freeze<T>(o: T): Readonly<T>;
Parameters
| Parameter | Description |
|---|
| obj | Required. The object to freeze. Primitives are returned as-is. |
Return Value
| Return Value | Description |
|---|
| object | The same object, now frozen. |
JavaScript Examples
Example 1 — Basic Use
Freeze a config object, then try to change it in non-strict mode.
var config = { api: "v1", debug: true };
Object.freeze(config);
config.debug = false;
console.log(config.debug); // true
How It Works
- Freeze first.
Object.freeze(config) locks the object. - Assignment ignored. Setting
debug to false does nothing in non-strict mode. - Value stays.
config.debug is still true.
Example 2 — Add and Delete Blocked
Frozen objects also reject new keys and deletes.
var config = { api: "v1", debug: true };
Object.freeze(config);
config.mode = "prod";
delete config.api;
console.log(config.mode); // undefined
console.log(config.api); // v1
How It Works
- No new keys. Adding
mode is blocked, so it stays undefined. - No deletes.
delete config.api fails, so api remains "v1". - Object stays put. A frozen object keeps its shape and values.
Example 3 — Check With isFrozen
Use Object.isFrozen() to see whether an object is locked.
var config = { api: "v1", debug: true };
Object.freeze(config);
console.log(Object.isFrozen(config)); // true
How It Works
- Freeze the object.
Object.freeze locks config. - Ask isFrozen.
Object.isFrozen(config) returns true. - Useful guard. Check before you assume an object can still be edited.
Compatibility
Browser & Runtime Support
Object.freeze() has been part of JavaScript since ES5 and is Baseline Widely available.
✓ Baseline · ES5
Object.freeze()
Supported in all modern browsers and Node.js. Also available in Internet Explorer 9+.
99% Modern browser support
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full supportObject.freeze Excellent
Bottom line: Use Object.freeze() for shallow locks. Remember nested objects stay mutable unless frozen too.
Frequently Asked Questions
It locks an object so you cannot add, remove, or change its own properties. It returns the same object.
No. Only the top-level object is frozen. Nested objects can still change unless you freeze them as well.
Call Object.isFrozen(obj). It returns true after a successful freeze.
seal() stops adding or removing properties but still allows changing existing values. freeze() also blocks value changes.
In strict mode, yes. In non-strict mode, forbidden changes fail silently and the object stays the same.
About the author
Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
8 people found this page helpful