How to freeze an Object in JavaScript?

In JavaScript, Object.freeze() makes an object immutable by preventing modifications to its properties. Once frozen, you cannot add, delete, or modify properties of the object.

Syntax

Object.freeze(obj)

Parameters

obj: The object to freeze. Returns the same object that was passed to the function.

How Object.freeze() Works

When an object is frozen:

  • New properties cannot be added
  • Existing properties cannot be removed
  • Existing property values cannot be changed
  • The object's prototype cannot be changed

Example: Basic Object Freezing

<html>
<body>
<script>
// Create an object with properties
var myObj = {
    prop1: 'original value',
    prop2: 42
};

// Freeze the object
Object.freeze(myObj);

// Try to modify existing property
myObj.prop1 = 'new value';

// Try to add new property
myObj.prop3 = 'added property';

// Display the object
document.write('prop1: ' + myObj.prop1 + '<br>');
document.write('prop2: ' + myObj.prop2 + '<br>');
document.write('prop3: ' + myObj.prop3);
</script>
</body>
</html>
prop1: original value
prop2: 42
prop3: undefined

Example: Checking if Object is Frozen

<html>
<body>
<script>
var obj1 = { name: 'John' };
var obj2 = { age: 25 };

// Freeze obj1
Object.freeze(obj1);

// Check if objects are frozen
document.write('obj1 frozen: ' + Object.isFrozen(obj1) + '<br>');
document.write('obj2 frozen: ' + Object.isFrozen(obj2));
</script>
</body>
</html>
obj1 frozen: true
obj2 frozen: false

Example: Freezing Arrays

<html>
<body>
<script>
var numbers = [1, 2, 3];

// Freeze the array
Object.freeze(numbers);

// Try to modify array
numbers[0] = 99;
numbers.push(4);

document.write('Array: ' + numbers.join(', '));
</script>
</body>
</html>
Array: 1, 2, 3

Shallow vs Deep Freezing

Object.freeze() performs shallow freezing. Nested objects remain mutable:

<html>
<body>
<script>
var user = {
    name: 'Alice',
    address: {
        city: 'New York',
        country: 'USA'
    }
};

Object.freeze(user);

// This won't work (shallow property)
user.name = 'Bob';

// This will work (nested object not frozen)
user.address.city = 'Los Angeles';

document.write('Name: ' + user.name + '<br>');
document.write('City: ' + user.address.city);
</script>
</body>
</html>
Name: Alice
City: Los Angeles

Common Use Cases

  • Configuration objects: Prevent accidental modification of settings
  • Constants: Create immutable data structures
  • API responses: Protect data integrity
  • Preventing bugs: Avoid unintentional object mutations

Comparison with Other Methods

Method Add Properties Delete Properties Modify Properties
Object.freeze() No No No
Object.seal() No No Yes
Object.preventExtensions() No Yes Yes

Conclusion

Object.freeze() provides a way to make objects immutable, preventing any modifications to their properties. Remember that freezing is shallow, so nested objects need to be frozen separately if complete immutability is required.

Updated on: 2026-03-15T23:18:59+05:30

222 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements