Object.freeze() / seal() Since: ES5(ECMAScript 2009)
Prevents modification of an object's properties. Object.freeze() fully freezes an object, while Object.seal() allows value changes but nothing else.
Syntax
// Freezes an object. Adding, deleting, and modifying properties are all prohibited. Object.freeze(object) // Seals an object. Adding and deleting properties are prohibited, but modifying values is allowed. Object.seal(object) // Returns whether the object is frozen. Object.isFrozen(object) // Returns whether the object is sealed. Object.isSealed(object)
Method List
| Method | Description |
|---|---|
| Object.freeze(object) | Freezes an object, prohibiting all additions, deletions, and value changes to its properties. Returns the frozen object. |
| Object.seal(object) | Seals an object, prohibiting property additions and deletions. Existing property values can still be modified. |
| Object.isFrozen(object) | Returns true if the object is frozen, or false otherwise. |
| Object.isSealed(object) | Returns true if the object is sealed, or false otherwise. |
Sample Code
// Freeze an object with Object.freeze.
var config = { apiUrl: "https://api.example.com", timeout: 3000 };
Object.freeze(config);
config.timeout = 5000; // Modification is silently ignored.
config.retryCount = 3; // Addition is silently ignored.
delete config.apiUrl; // Deletion is silently ignored.
console.log(config.timeout); // Outputs 3000 (unchanged).
console.log(config.retryCount); // Outputs undefined.
// Seal an object with Object.seal.
var settings = { theme: "dark", lang: "ja" };
Object.seal(settings);
settings.theme = "light"; // Value modification is allowed.
settings.fontSize = 14; // Addition is silently ignored.
delete settings.lang; // Deletion is silently ignored.
console.log(settings.theme); // Outputs "light".
console.log(settings.fontSize); // Outputs undefined.
// Check frozen/sealed status.
console.log(Object.isFrozen(config)); // Outputs true.
console.log(Object.isSealed(config)); // A frozen object is also sealed, so outputs true.
console.log(Object.isSealed(settings)); // Outputs true.
console.log(Object.isFrozen(settings)); // Outputs false.
Differences Between freeze and seal
| Operation | Object.freeze() | Object.seal() |
|---|---|---|
| Add property | Not allowed. | Not allowed. |
| Delete property | Not allowed. | Not allowed. |
| Modify value | Not allowed. | Allowed. |
Overview
Object.freeze() and Object.seal() are methods that restrict modifications to an object. Object.freeze() is the strictest option, prohibiting all property additions, deletions, and value changes. Object.seal() only prohibits additions and deletions, while still allowing value changes.
Note that both methods apply only a shallow restriction. Nested objects are not frozen or sealed, so properties on inner objects can still be modified. To fully freeze an object including nested ones, you need to apply Object.freeze() recursively.
Attempts to modify a frozen or sealed object are silently ignored with no error. However, in strict mode ("use strict"), a TypeError is thrown, making unintended modifications easier to detect. These methods are effective for data that must not change during program execution, such as configuration objects and constants.
Browser Compatibility
5 or earlier ×
3.5 or earlier ×
5 or earlier ×
8 or earlier ×
11 or earlier ×
Android Browser
37+ ○
4 or earlier ×
Chrome Android
36+ ○
17 or earlier ×
Firefox Android
79+ ○
3 or earlier ×If you find any errors or copyright issues, please contact us.