Object.freeze
Freezes an object, preventing new properties from being added, existing properties from being removed, and existing properties from being changed
Syntax
Object.freeze(obj)Parameters
| Parameter | Type | Description |
|---|---|---|
| obj | T | The object to freeze |
Return Value
The same object that was passed in
Examples
const obj = { name: 'Alice', age: 30 };
Object.freeze(obj);
obj.age = 31; // silently fails in non-strict mode
console.log(obj.age); // 30const CONFIG = Object.freeze({
API_URL: 'https://api.example.com',
TIMEOUT: 5000,
});
// CONFIG.API_URL = 'x'; // TypeError in strict modeconst arr = Object.freeze([1, 2, 3]);
// arr.push(4); // TypeError
console.log(arr); // [1, 2, 3]Understanding Object.freeze
The Object.freeze method in JavaScript freezes an object, preventing new properties from being added, existing properties from being removed, and existing properties from being changed. It belongs to the Object object and is one of the most widely used methods for working with object values in modern JavaScript and TypeScript applications.
The method signature is Object.freeze(obj). It accepts 1 parameter: obj. When called, it returns the same object that was passed in. Understanding when and how to use freeze() helps you write more expressive, readable code.
Common use cases for Object.freeze include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like object-isfrozen, object-seal, object-issealed, enabling you to chain operations together for complex data manipulation pipelines.
Browser support for Object.freeze is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.
Browser Compatibility
Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.
Related Methods
More Object Methods
Other methods in the Object object
Related Tools
More Object Methods
Explore JavaScript Methods
Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.