Table of contents
1.
Introduction
2.
What Object.freeze() does
3.
How to make objects immutable?
4.
FAQs
5.
Key Takeaways
Last Updated: Mar 27, 2024
Easy

What object.freeze() does, How to make objects immutable?

Author Malay Gain
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

A object in javascript is immutable when its properties can’t be modified. To prevent accidental changes of objects, we need to ensure the immutability of objects. In javascript, we mainly use three functions - Object.freeze( ), Object.seal( ), Object.preventExtensions( ). Here we will limit our discussion to Object.freeze( ) function that preserves the enumerability, writability, configurability, and prototype of the object.

We will first discuss how Object.freeze( ) function works. Then we will see how these functions are used to make an object immutable. 

What Object.freeze() does

In javascript, Object.freeze( ) function freezes an object. Freezing an object means preventing from new properties being added and existing properties from being removed. It also prevents changing the values of the existing properties of the object.

Object.freeze() method

Object.freeze( ) is one of the built-in object constructor static methods that prevents from removing or altering existing properties. It preserves the enumerability, writability, configurability, and prototype of the object by returning the passed object not its frozen copy.

Syntax

Object.freeze(obj)
You can also try this code with Online Javascript Compiler
Run Code

Here, parameter obj is the object that needs to be frozen.

It returns the same object without making its copy.

Use cases

  • This function is used to freeze objects and arrays.
  • It is also used to make objects immutable.

Example

class Student
{
    constructor(name, enr_no)
    {
        this.name=name;
        this.enr_no=enr_no;
    }
};
var student1 = new Student("Ninja",510519);
Object.freeze(student1); //frozen the object
student1.name = "Dhruv" //this assignment doesn’t affect
console.log(student1.name)
You can also try this code with Online Javascript Compiler
Run Code

Output

Ninja //value of the name property is unchanged

Above example shows how value of the property is unaltered after freezing the object.

Freezing an array

var arr=[5,3,4,1]
Object.freeze(arr);
arr.push(2);
You can also try this code with Online Javascript Compiler
Run Code

Output

TypeError: Cannot add property 4, object is not extensible

How to make objects immutable?

An object is mutable if its value or property can be modified. So, an object is immutable when we can't alter its properties or its values.

The necessity of immutability

In javascript, if we create an object obj1, a copy of the object can be created by just assigning obj1 to another variable obj2. But here reference of the obj1 is copied, not its values. So, making any changes to obj2 will reflect on obj1. So, accidental modification can lead to unexpected bugs. For this reason, we need the immutability of objects to prevent accidental changes.

Example

In javascript, mainly Object.freeze() and Object.seal() these two functions are used to make objects immutable. We have already seen how Object.freeze( ) works.

Object.seal( ) also similar type of function. But it provides partial immutability as values of the existing property can be modified. But existing properties can not be removed, or new properties can not be added.

FAQs

1. What is shallow freeze?

Object.freeze() doesn’t work on nested object. So, if there is an object inside an object, then the inner one can’t be frozen by freezing the outer object. That’s why Object.freeze() is called shallow freeze.

2. What is Object.seal( ) function?

Using Object.seal( ), we can make an object partially immutable. If we pass an object to this function, existing properties of the object can not be removed or new properties can not be added. But values of the existing property can be modified.

Key Takeaways

This article covered the immutability of objects in the context of the object.freeze().

Check out the Coding Ninjas Studio library for getting a better hold of the data structures and algorithms and the guided path for learning javascript from basics.

Side by side, you can also practice a wide variety of coding questions commonly asked in interviews in Coding Ninjas Studio. Along with coding questions, you can also find the interview experience of scholars working in renowned product-based companies here. 

Happy learning!

Live masterclass