How to check two objects have same data using JavaScript ? Last Updated : 01 Jul, 2020 Comments Improve Suggest changes Like Article Like Report In JavaScript, we cannot directly compare two objects by equality operators (double equals == or triple equals ===) to see whether they are equal or not. Comparing two objects like this results in false even if they have the same data. It is because those are two different object instances, they are referring to two different objects. There is no direct method in javascript to check whether two objects have the same data or not. Below is the pseudo-code of the function, followed by the code snippet: Pseudo Code: function hasSameData if both objects have the same number of keys (properties) if every key of obj1 matches with the corresponding key of obj2 and values of the same keys of both objects match. return true return false Approach: We will find keys of both objects by Object.keys(), which returns an array of keys of that object. For checking that every key in obj1 is also present in obj2, and if values of those key matches, we will use every() method. The every() method accepts a callback and returns "true" or "false" according to the callback condition. Example: javascript <script> const obj1 = { name: 'Ram', age: 21 }; const obj2 = { name: 'Ram', age: 21 }; const haveSameData = function (obj1, obj2) { const obj1Length = Object.keys(obj1).length; const obj2Length = Object.keys(obj2).length; if (obj1Length === obj2Length) { return Object.keys(obj1).every( key => obj2.hasOwnProperty(key) && obj2[key] === obj1[key]); } return false; } document.write(haveSameData(obj1, obj2)); </script> Output: true Note: The above approach does not work for nested objects (Objects and arrays inside an object). In such cases, it needs function according to the nested object. Applying the above function would fail in case of nested objects as shown below:Â Â javascript <script> const obj1 = { name: 'Ram', age: 21, hobbies: ['Cricket', 'Swimming'] }; const obj2 = { name: 'Ram', age: 21, hobbies: ['Cricket', 'Swimming'] }; const haveSameData = function(obj1, obj2) { const obj1Length = Object.keys(obj1).length; const obj2Length = Object.keys(obj2).length; if(obj1Length === obj2Length) { return Object.keys(obj1).every( key => obj2.hasOwnProperty(key) && obj2[key] === obj1[key]); } return false; } document.write(haveSameData(obj1, obj2)); </script> Output: false Comment More infoAdvertise with us Next Article How to check two objects have same data using JavaScript ? P prerak_jain Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Misc Similar Reads How to check two elements are same using jQuery/JavaScript ? Given an HTML document containing two elements and the task is to check whether both elements are same or not with the help of JavaScript. Approach 1: Use is() method to check both selected elements are same or not. It takes an element as argument and check if it is equal to the other element. Examp 2 min read How to Check an Object is Empty using JavaScript? These are the following ways that can be used to Check an Object is Empty using JavaScript: 1. Using Object.keys() Method - Mostly usedThe Object.keys() method returns an array that contains the property names of an object. If the length of array is 0, then object is empty.JavaScriptlet obj = {}; if 2 min read How to compare two JavaScript array objects using jQuery/JavaScript ? In this article, we are given two JavaScript array/array objects and the task is to compare the equality of both array objects. These are the methods to compare two JavaScript array objects: Using jQuery not() methodUse the sort() functionUse JSON.stringify() functionUsing every() and indexOf()Using 3 min read How to check a JavaScript Object is a DOM Object ? Checking if a JavaScript object is a DOM object involves verifying whether the object represents an element or component within the Document Object Model (DOM) of an HTML or XML document. This can be done by checking if the object is an instance of Node, Element, or other specific DOM interfaces.Wha 2 min read How to compare Arrays of Objects in JavaScript? In JavaScript, comparing arrays of objects can be more complex than comparing primitive data types. We will discuss different ways to compare arrays of objects effectively, with detailed code examples and explanations.Syntax: Before going to detail the comparison techniques, let's first understand h 5 min read How to Compare Two Objects using Lodash? To compare two objects using lodash, we employ Lodash functions such as _.isEqual(), _.isMatch(), and _.isEqualWith(). These methods enable us to do a comparison between two objects and determine if they are equivalent or not.Below are the approaches to do a comparison between two objects using Loda 4 min read How to Detect an Undefined Object Property in JavaScript ? Detecting an undefined object property is the process of determining whether an object contains a certain property, and if it does, whether the value of that property is undefined. This is an important concept in JavaScript programming, as it helps to prevent errors that can occur when attempting to 3 min read How to check whether an object exists in javascript ? Checking whether an object exists in JavaScript refers to determining if a variable or property has been declared and contains a valid value. This helps avoid errors when accessing or manipulating objects that may be undefined, null, or not initialized properly.Here we have some common approaches to 3 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 How to Check a Key Exists in JavaScript Object? Here are different ways to check a key exists in an object in JavaScript.Note: Objects in JavaScript are non-primitive data types that hold an unordered collection of key-value pairs. check a key exists in JavaScript object1. Using in Operator The in operator in JavaScript checks if a key exists in 2 min read Like