JavaScript

How to freeze an object in JavaScript

Have you ever been caught up in a situation where it is required to make an immutable JavaScript object and prevent it from changing the values of existing properties? If that’s the case, utilize the “Object.freeze()” method in your JavaScript program and make it an immutable data type.

What is Object.freeze() method

The “Object.freeze()” method is used to freeze an already declared object and restrict it all from manipulation such as adding new properties, removing existing properties, and updating the values of the existing properties.

This post will discuss the method to freeze an object in JavaScript. So, let’s start!

How to freeze an object in JavaScript

By default, JavaScript objects are of “mutable” data type, which means that their values can be changed over time. However, you can make it “immutable” by declaring it as a “constant”.

When an object is created using the “const” keyword, you can still individually reassign or change the values of its properties, but being an immutable data type, JavaScript does not allow you to reassign the whole object to something else.

Have a look at the following example to understand the stated concept.

Example

For instance, we will create an object named “employee” having the following properties:

const employee = {

age: 30,

name: "Alex"

};

console.log(employee);

After doing so, we will try to update the values of the “employee” object as a whole:

employee = {

age: 35,

name: "Paul"

};

console.log(employee);

As you can see, the execution of the specified operation output a “TypeError” which states that assignment to an already declared constant variable is not possible:

Now, we will check the other case by individually reassigning a value to the employee object’s “name” property:

employee.name= "Max";

console.log("After changing the employee.name property value");

console.log(employee);

The given output signifies that the “employee.name” property value is updated to “Max”:

Now, it is proved that although an object becomes “immutable” with the help of the “const” keyword, it still permits you to individually reassign its property values.

You can “freeze” an object in a situation where it is needed to restrict the object from updating the existing properties or addition of new properties.

Want to do so? Follow the below-given section to know more about freezing an object in JavaScript.

How to freeze an object in JavaScript using Object.freeze() method

The “Object.freeze()” method is utilized to freeze an already declared object. When an object gets freezed, it prevents the deletion of the existing object properties, addition of new properties, updation of the enumerability, writability, and configurability of existing properties. Moreover, you cannot change the object prototype and value of the existing properties after freezing the related object.

Syntax

Object.freeze(obj)

Here, “obj” represents the JavaScript object which will be freezed with the help of the “Object.freeze()” method.

Example: How to freeze an object in JavaScript using Object.freeze() method

First of all, we will freeze the “employee” object by using the “Object.freeze()” method:

Object.freeze(employee);

At the time of freezing the “employee” object, the value of “employee.age” is “30,” and the “employee.name” is set as “Max”:

In the next step, we will verify if the “employee” object is freezed or not. For this purpose, JavaScript offers “Object.isFrozen()” built-in method that accepts a JavaScript “object” as an argument and returns “true” if the passed object is freezed, otherwise the return case of “Object.isFrozen()” method will be set to “false”:

Object.isFrozen(employee);

Output

The value returned by the “Object.isFrozen()” method is “true,” which indicates that the “employee” object is successfully freezed.

Updating freezed object property in strict mode vs non-strict mode

Now, we will try to update the “employee.name” property value to “Paul”:

employee.name= "Paul";

console.log(employee);

If you are in “non-strict” mode, then the specified operation of updating value will fail silently:

However, in the case of “strict mode”, a “TypeError” will be also displayed on the console, while terminating the updation operation of the freezed “employee” object:

"use strict";

employee.name= "Paul";

console.log(employee);

Output

Deleting freezed object property in strict mode vs non-strict mode

Similarly, you cannot delete an existing property of a freezed object:

delete employee.name;

console.log(employee);

The above-given code will try to remove the “name” property of the “employee” object and fail silently in “non-strict” mode:

Whereas, in “strict mode”, a “TypeError” will be shown on the console, which states that you cannot delete the “name” property of the “employee” object that we have freezed with the help of the “Object.freeze()” method:

"use strict";

delete employee.name;

console.log(employee);

Output

That was all essential information related to freezing an object in JavaScript. You can investigate further according to your preferences.

Conclusion

The JavaScript Object.freeze() method is utilized to freeze a declared object. When an object gets freezed, it prevents the deletion of the existing object properties, addition of new properties, updation of the enumerability, writability, and configurability of existing properties. Moreover, you cannot change the object prototype and value of the existing properties after freezing it. This write-up discussed the method to freeze an object in JavaScript.

About the author

Sharqa Hameed

I am a Linux enthusiast, I love to read Every Linux blog on the internet. I hold masters degree in computer science and am passionate about learning and teaching.