Immutable Objects in Javascript

An immutable object is one whose state cannot and will not change after it’s initial creation.

Assuming that you already know the importance of the immutable object. let's dive directly into how to achieve that.

What is an object in javascript? : Object in javascript can be represented by an entity having different properties and behaviour/methods.

let name = {
firstName: "Virender",
lastName: "Sehwag",
getFullName() {
return firstName + " " + lastName;
},
};

By Immutability, I am not referring to const in javascript. As arrays and objects defined with const can be changed like

const arr = [1,2,3];
arr[3] = 4;
console.log(arr); // [1,2,3,4];

const obj = {firstName="Virat"}
obj.lastName = "Kohli";
console.log(obj); // {firstName="Virat", lastName="Kohli"}

I am talking about complete immutability.

We can achieve immutability by 3 different methods and of different level.

Object.freeze()

If we pass an object in this method then we cannot add a new property, delete a property, or even update a current property.

Trying to make any changes mentioned above after an object being frozen will fail and will throw errors in strict mode.

let name = {
firstName: "Virender",
lastName: "Sehwag",
getFullName() {
return firstName + " " + lastName;
},
};
Object.freeze(name);name.middleName = "R"; // !!!ERROR!!! in strict mode
name.firstName = "Sachin"; // ERROR!!! in strict mode

Object.isFrozen() is an Object method to check if an object is frozen by passing the object as the argument.

Object.isFrozen(name);    // true

Object.seal()

This method seals the current object properties and methods. We cannot add any new properties and methods but we can change the current properties and methods.

These sealed object properties cannot be deleted.

let name = {
firstName: "Virender",
lastName: "Sehwag",
getFullName() {
return firstName + " " + lastName;
},
};
Object.seal(name);name.middleName = "R"; // ERROR!!! in strict mode
name.firstName = "Sachin"; // Works!!!
delete name.lastName; // ERROR!!! in strict mode

Object.isSealed() is an Object method to check if an object is sealed by passing the object as the argument.

Object.isSealed(name);   // true

Object.preventExtension()

This is similar to Object.seal() in terms that in this also we cannot add any new property or method but we can change the existing once.

The difference between Object.seal() and Object.preventExtension() is that in preventExtesion() we can delete the current properties.

let name = {
firstName: "Jasprit",
lastName: "Bumrah",
getFullName() {
return firstName + " " + lastName;
},
};
Object.preventExtesion(name);name.middleName = "R"; // ERROR!!! in strict mode
name.firstName = "Sachin"; // Works!!!
delete name.lastName // true

Object.isExtensible() is an Object method to check if an object is extensible by passing the object as the argument.

Object.isExtensible(name);    // false

So as per the need, we can use these methods.

Full stack developer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store