How to check if two objects are equal in JavaScript

Hello Geeks in this article we will see how you can check whether two objects are equal or not in JavaScript. As we know that objects are of reference type so we cannot directly use === or == operator to compare two objects. Like we can compare two primitive values in JavaScript as:

‘X’ == ‘X’ //True

10 == 13 //False

Comparing two primitive values is comparatively easy when we compare two Objects in JavaScript. Here in this article we will discuss 3 methods using which you can easily compare two Objects in JavaScript.

  1. Using JSON.stringify()
  2. Using lodash Library isEqual function
  3. Manual comparison
How to check if two objects are equal in JavaScript

Using JSON.stringify()

Using the JSON.stringify() method we can convert a JSON object to a string and then comparing two strings is easier than comparing two objects.

 Example Source Code:

const fruit_1 = { 
    fruit: '🥭',
    energy: '150cal'
};
const fruit_2 = { 
    fruit: '🥭',
    energy: '150cal'
};

// Using JavaScript
var x = (JSON.stringify(fruit_1) === JSON.stringify(fruit_2));
console.log(x);  //true

Output:

true

Explanation:

In this code we made two objects named fruit_1 and fruit_2 then passing both the object in JSON.stringify() method which will convert these objects to strings. And then comparing these strings using “===” operator and saving the boolean result into a container/variable x. Finally, we are printing the result using console.log() function.

You can anytime check the type of variable using typeof() method. In the code above, let say we want to check whether the JSON.stringify() making it string or not use the below code:

const fruit_1 = { 
    fruit: '🥭',
    energy: '150cal'
};
const fruit_2 = { 
    fruit: '🥭',
    energy: '150cal'
};

var obj = JSON.stringify(fruit_1);
var x = (JSON.stringify(fruit_1) === JSON.stringify(fruit_2));

console.log(typeof(fruit_1)); //Object
console.log(typeof(obj));     //String
console.log(typeof(x));       //Boolean

This code will print object, string and boolean respectively as fruit_1 is object, then obj is String and x is containing a boolean value.

Using lodash Library isEqual function

JavaScript also has a lightweight library called lodash. Using isEqual() function of lodash we can compare two objects are equal or not in JavaScript. Let’s understand the concept by examples.

Syntax:

_.isEqual(object_1, object_2);

Here isEqual function has two parameters object_1 and object_2. These are the objects that we want to compare.

Return Type:

Boolean (true or false)

Example Source Code:

const fruit_1 = { 
    fruit: '🥭',
    energy: '150cal'
};
const fruit_2 = { 
    fruit: '🥭',
    energy: '150cal'
};
// Using lodash library isEqual() function
var isFruitEqual = _.isEqual(fruit_1, fruit_2);
console.log(isFruitEqual); // true

Output:

true

Explanation:

In the above code example, we created two fruit objects fruit_1 and fruit_2 both have the same data content. we are using isEqual() function of lodash library to compare two objects this functions takes two arguments and comapre both of them and then returns a boolean result. We are storing the returned boolean result in a variable isFruitEqual and finally printing with the help of console.log() function.

Manual comparison

We can also manually compare each JavaScript Object property one by one. This is a very lengthy process and not a recommended practice to compare two JavaScript Objects.

Example Source Code:

function isPersonSame(person_1, person_2) {
  return person_1.name === person_2.name;
}
const p_1 = {
  name: 'John Doe'
};
const p_2 = {
  name: 'Alberto'
};
const p_3 = {
  name: 'John Doe'
};
console.log(isPersonSame(p_1, p_2)); // => false
console.log(isPersonSame(p_1, p_3)); // => true

Output:

flase
true

Explanation:

In the above code example, we created a function isPersonSame() which will tell two person names are same or not. We have created 3 person objects p_1, p_2 and p_3 out of them two person objects P_1 and p_3 are same. Then we are comparing each of them with the property name. Finally, printing the result using console.log() function.

Nested Comparison:

Using the isEqual function also we can also perform nested comparisons. Let’s understand by example:

Example Source Code:

const fruit_1 = {
  fruit: '🍒',
  nutrients: {
    energy: '100Cal',
    minerals: {
      name: 'Protein',
    },
  },
};

const fruit_2 = {
  fruit: '🍓',
  nutrients: {
    energy: '200Cal',
    minerals: {
      name: 'Carbs',
    },
  },
};

// Using lodash library isEqual() function
var isFruitEqual = _.isEqual(fruit_1 , fruit_2); 
console.log(isFruitEqual); // false

Output:

false

Conclusion

So these are the methods by which we can compare two objects are equal or not in JavaScript. We hope these methods are easy to understand if still you are facing any issue with any of the given methods in this article feel free to let us know via comment section below. We will provide support as soon as possible.

Write for us
GeekyBeginners articles are written by software geeks like you. If you also would like to contribute to GeekyBeginners by writing paid articles, you can check the write for us page.

Leave a Comment