Objects are the reference type in JavaScript and are also named value pairs, where the value may determine a property or behaviour. They are used extensively because nowadays the web development has changed vastly. This article describes how to compare two JavaScript objects in the following formats:
- Comparing JavaScript Objects based on reference
- Comparing Two JavaScript Objects based on the data it contains
Method 1: Comparing two objects based on reference: The strict equals (===) operator compares memory locations in case of comparing objects.
Example:
JavaScript
<script> // Creating two Objects with same data object1 = { name: "GeeksForGeeks", founder: "SandeepJain" }; object2 = { name: "GeeksForGeeks", founder: "SandeepJain" }; // In this case, obj1 and obj2 // have different memory locations // therefore === returns false check = (object1 === object2); document.write("Does Object1 " + "equals Object2 ? : " + check); // Going to the next line document.write("< p > </ >"); // Pointing to same reference now object1 = object2; check = (object1 === object2); document.write("Does Object1 equals" + " Object2 ? : " + check); </script> |
Output:

Method 2: Comparing Two JavaScript Objects based on the data:
Example 1:
JavaScript
<script> <script> // Creating two objects with // a method inside it object1 = { name: "GeeksForGeeks", author: "Jagannath", greet: () => { document.write("Hello Geeks"); } }; object2 = { name: "GeeksForGeeks", author: "Jagannath", greet: () => { document.write("Hello Geeks"); } }; string1 = JSON.stringify(object1); string2 = JSON.stringify(object2); check = (string1 === string2); document.write("Is the data same in" + " Object1 and Object2 ?: " + check); </script> |
Output:

Example 2:Drawback of using JSON.stringify() method. If the order of data inside an object changes, then the above method does not work properly. This is the drawback of using JSON.stringify() method as demonstrated below.
JavaScript
<script> // Creating two objects with // a method inside it object1 = { name: "GeeksForGeeks", author: "Jagannath", greet: () => { document.write("Hello Geeks"); } }; // Changing the order of the // values defined in the // second object object2 = { author: "Jagannath", name: "GeeksForGeeks", greet: () => { document.write("Hello Geeks"); } }; string1 = JSON.stringify(object1); string2 = JSON.stringify(object2); check = (string1 === string2); document.write("Is the data same in " + "Object1 and Object2 ?: " + check); </script> |
Output:

Recommended Posts:
- JavaScript SyntaxError - Test for equality (==) mistyped as assignment (=)?
- How to compare two JavaScript array objects using jQuery/JavaScript ?
- Extract unique objects by attribute from array of objects.
- How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ?
- How to Compare Equality of Struct, Slice and Map in Golang?
- SASS | Equality Operators
- JavaScript Course | Objects in JavaScript
- How to merge properties of two JavaScript objects dynamically?
- How to check two objects have same data using JavaScript ?
- Creating objects in JavaScript (4 Different Ways)
- JavaScript | Objects
- Objects in Javascript
- Sort an array of objects using Boolean property in JavaScript
- Sort array of objects by string property value in JavaScript
- Max/Min value of an attribute in an array of objects in JavaScript
- JavaScript | Date Objects
- How to remove duplicates from an array of objects using JavaScript ?
- How to remove Objects from Associative Array in JavaScript ?
- How to Sort/Order keys in JavaScript objects ?
- How to remove object from array of objects using JavaScript ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


