Print Difference between Two Objects in JavaScript ? Last Updated : 22 Feb, 2024 Comments Improve Suggest changes Like Article Like Report Printing the difference between two objects in JavaScript involves comparing the properties of the two objects and identifying the discrepancies between them. This can include properties that exist in one object but not in the other, as well as properties with differing values. These are the following ways: Table of Content Using for loopUsing Object.entries() and Array.prototype.reduce()Using for loopThis approach involves iterating over the properties of one object and checking if they exist or have different values in the other object. You can accomplish this by using a loop (such as for...in) to iterate through the properties of one object and compare them with the corresponding properties in the other object. Example: The findDifference function compares two objects and returns an object representing their differences. It checks keys in both objects, noting variations in values. The output highlights discrepancies between the objects, including missing keys. JavaScript function findDifference(obj1, obj2) { const diffKeys = []; for (const key in obj1) { if (!(key in obj2) || obj1[key] !== obj2[key]) { diffKeys.push(key); } } for (const key in obj2) { if (!(key in obj1) || obj1[key] !== obj2[key]) { if (!diffKeys.includes(key)) { diffKeys.push(key); } } } return diffKeys; } const obj1 = { a: 1, b: 2, c: 3 }; const obj2 = { a: 1, b: 4, d: 5 }; console.log(findDifference(obj1, obj2)); Output[ 'b', 'c', 'd' ] Using Object.entries() and Array.prototype.reduce()ES6 introduced several powerful features for working with objects and arrays, such as Object.entries() and Array.prototype.reduce(). Using these features, you can iterate over the properties of an object and compare them with another object. Example: The Function takes two objects (obj1 and obj2) as arguments and returns an object representing the differences between them. It iterates through the keys of both objects, comparing their values. If a key exists in one object but not the other, or if the values of the same key differ, it records the difference. The final result is an object containing the discrepancies found in both objects. JavaScript function findDifference(obj1, obj2) { const diffKeys = Object.keys(obj1) .reduce((acc, key) => { if (!(key in obj2) || obj1[key] !== obj2[key]) { acc.push(key); } return acc; }, []); Object.keys(obj2).forEach(key => { if (!(key in obj1) || obj1[key] !== obj2[key]) { if (!diffKeys.includes(key)) { diffKeys.push(key); } } }); return diffKeys; } const obj1 = { a: 1, b: 2, c: 3 }; const obj2 = { a: 1, b: 4, d: 5 }; console.log(findDifference(obj1, obj2)); Output[ 'b', 'c', 'd' ] Comment More infoAdvertise with us Next Article Print Difference between Two Objects in JavaScript ? A anjugaeu01 Follow Improve Article Tags : JavaScript Web Technologies Similar Reads Difference Between Variables and Objects in JavaScript The variables and objects are fundamental concepts but they serve different purposes. The Variables are used to store data values while objects are used to group related data and functions into a single entity. JavaScript VariableA variable in JavaScript is a named container that stores a value. It 2 min read Get the Difference between Two Sets using JavaScript To get the difference between two sets in JavaScript, you need to identify elements present in the first set but not in the second. This involves iterating over the first set and filtering out elements that are also found in the second set, ensuring efficient comparison. We can get the difference be 2 min read How to print the content of an object in JavaScript ? To print the content of an object in JavaScript we will use JavaScript methods like stringify, object.values and loops to display the required data. Let's first create a JavaScript Object containing some key-values as given below: JavaScript // Given Object const obj = { name: 'John', age: 30, city: 3 min read How to create object properties in JavaScript ? JavaScript is built on an object-oriented framework. An object is a collection of properties, where each property links a key to a value. These properties are not in any specific order. The value of a JavaScript property can be a method (function). Object properties can be updated, modified, added, 4 min read How to Extend an Object in JavaScript ? Extending an object in JavaScript means adding properties or methods to enhance its functionality. This can be done dynamically. The extends keyword is used to create a subclass from a parent class, enhancing object-oriented programming flexibility in JavaScript. Syntax:class childclass extends pare 2 min read Equality for two JavaScript objects 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 ch 2 min read Creating objects in JavaScript An object in JavaScript is a collection of key-value pairs, where keys are strings (properties) and values can be any data type. Objects can be created using object literals, constructors, or classes. Properties are defined with key-value pairs, and methods are functions defined within the object, e 5 min read How to iterate over a JavaScript object ? Iteration involves looping through the object's properties one by one. Depending on the method used, you can access and manipulate different levels of properties efficiently. Here are several methods.There are many methods to iterate over an object which are discussed below: Table of ContentUsing fo 3 min read How to create an object with prototype in JavaScript ? In this article, we will discuss object creation & prototypes, along with understanding the different ways for object creation & their implementation through the examples. Prototypes are the mechanism by which objects in JavaScript inherit features from another object. A prototype property i 4 min read How to get dynamic access to an object property in JavaScript ? In JavaScript, an object is a collection of properties, where each property consists of a key-value pair. Objects are a fundamental data type in JavaScript. You can create an object in JavaScript in the following ways: By using the object literal notation, which is a comma-separated list of key-valu 7 min read Like