Equality for two JavaScript objects Last Updated : 24 Jul, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report Comparing two JavaScript objects for equality involves checking if they have the same properties with the same values. This process is more complex than comparing primitive values due to the objects' reference-based nature and potential nested structures in JavaScript. Below are the approaches to check the equality of two JavaScript objects:Table of ContentUsing a Strict equality operatorUsing the Object.is methodComparing JSON StringsUsing Lodash's _.isEqual MethodUsing a Strict equality operatorThe strict equals (===) operator compares memory locations in case of comparing objects.Example: To demonstrate comparing the equality of the two JavaScript objects using the strict equality operator in JavaScript. JavaScript object1 = { name: "GeeksForGeeks", founder: "SandeepJain", }; object2 = { name: "GeeksForGeeks", founder: "SandeepJain", }; check = object1 === object2; // Nikunj Sonigara console.log("Does Object1 " + "equals Object2 ? : " + check); object1 = object2; check = object1 === object2; console.log("Does Object1 equals" + " Object2 ? : " + check); OutputDoes Object1 equals Object2 ? : false Does Object1 equals Object2 ? : true Using the Object.is methodObject.is is similar to the strict equality operator (===) but treats NaN as equal to NaN and -0 not equal to 0.Example: To demonstrate checking the equality of the two JavaScript objects. JavaScript const obj1 = { a: 1 }; const obj2 = { a: 1 }; console.log(Object.is(obj1, obj2)); Outputfalse Comparing JSON StringsThis method involves converting objects to JSON strings and comparing the strings. It works well for simple, non-circular objects.Example: To demonstrate checking the equality of two JavaScript objects by converting the Objects to JSON strings. JavaScript const obj1 = { a: 1, b: 2 }; const obj2 = { a: 1, b: 2 }; const isEqual = JSON.stringify(obj1) === JSON.stringify(obj2); console.log(isEqual); Outputtrue Using Lodash's _.isEqual MethodLodash's _.isEqual method can be used to compare two objects for deep equality. This method simplifies the comparison process and handles nested objects efficiently. JavaScript const _ = require('lodash'); const obj1 = { a: 1, b: { c: 2 } }; const obj2 = { a: 1, b: { c: 2 } }; const obj3 = { a: 1, b: { c: 3 } }; console.log(_.isEqual(obj1, obj2)); console.log(_.isEqual(obj1, obj3)); Output:truefalse Comment More infoAdvertise with us Next Article What is object equality in JavaScript ? V venkata_jagannath_gannavarapu Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Misc JavaScript-Questions Similar Reads What is object equality in JavaScript ? JavaScript provides us with a number of ways to check if two objects are equal. Let us demonstrate how to check whether two objects are equal. There are three types of equality - Referential equality.Shallow equality.Deep equality.Referential equality: We can say two objects are referentially equal 4 min read How to Compare Objects for Equality in JavaScript? When working with JavaScript it is common to compare objects for equality. we will explore various approaches to compare objects for equality in JavaScript.These are the following approaches:Table of ContentUsing JSON.stringifyUsing Lodash's isEqual() MethodUsing Object.keys() and Array.every()Using 2 min read How to Clone a JavaScript Object? Here are different methods to clone a JavaScript object.1. Using Object.assign() MethodThe Object.assign() method is used to clone the given object. We have assigned the given object to the new empty object by just using this method. Syntaxlet Clone_object =Object.assign({}, sourceObject);Note: This 2 min read JavaScript - Create an Object From Two Arrays Here are the different methods to create an object from two arrays in JavaScript1. Using for-each loopThe arr.forEach() method calls the provided function once for each element of the array. JavaScriptconst a1 = ['name', 'age', 'city']; const a2 = ['Ajay', 25, 'New Delhi']; const res = {}; a1.forEac 3 min read Objects in Javascript An object in JavaScript is a data structure used to store related data collections. It stores data as key-value pairs, where each key is a unique identifier for the associated value. Objects are dynamic, which means the properties can be added, modified, or deleted at runtime.There are two primary w 4 min read How to Compare Objects in JavaScript? Comparing objects is not as simple as comparing numbers or strings. Objects are compared based on their memory references, so even if two objects have the same properties and values, they are considered distinct if they are stored in different memory locations. Below are the various approaches to co 3 min read Like