What is object equality in JavaScript ? Last Updated : 21 Jul, 2023 Comments Improve Suggest changes Like Article Like Report 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 when the pointers of the two objects are the same or when the operators are the same object instance. We can check referential equality in 3 ways: === (triple equals) operator or the strict equality operator. Strictly equality refers to the equality of two values. If the two values have the same type, they are considered equal.== (double equals) is the loose equality operator. It converts both values to a common type and then checks for equality.object.is() function.== operator: JavaScript let a = 1; let b = 1; console.log(a == b); // true let c = 10; let d = "10"; console.log(c == d); // true const name1 = { first_name: "sarah", }; const name2 = { first_name: "sarah", }; console.log(name1 == name2); // false Outputtrue true false === operator: JavaScript let a = 1; let b = 1; console.log(a === b); // true let c = 10; let d = "10"; console.log(c === d); // false const name1 = { first_name: "sarah", }; const name2 = { first_name: "sarah", }; console.log(name1 === name2); // false Outputtrue false false object.is() Method: JavaScript let a = 1; let b = 1; let c = 10; let d = "10"; console.log(Object.is(c, d)); // false console.log(Object.is(a, b)); // true console.log(Object.is(a, a)); // true const name1 = { first_name: "sarah", }; const name2 = { first_name: "sarah", }; console.log(Object.is(name1, name2)); // false console.log(Object.is(name1, name1)); // true Outputfalse true true false trueShallow equality: A shallow equality check of objects returns the list of properties of both objects and further checks if the properties are equal.Shallow equality is a type of equality that occurs when the key values in both objects are equal.Example: In this example, the shallowEqual function compares the properties of obj1 and obj2. Since both objects have the same properties "a" and "b" with the same values, the function returns true for the comparison of obj1 and obj2 JavaScript function shallowEqual(obj1, obj2) { const keys1 = Object.keys(obj1); const keys2 = Object.keys(obj2); if (keys1.length !== keys2.length) { return false; } for (let key of keys1) { if (!obj2.hasOwnProperty(key) || obj1[key] !== obj2[key]) { return false; } } return true; } const obj1 = { a: 1, b: 2 }; const obj2 = { a: 1, b: 2 }; const obj3 = { a: 1, b: 2, c: 3 }; console.log(shallowEqual(obj1, obj2)); // Output: true (Both objects have the same properties and values) console.log(shallowEqual(obj1, obj3)); // Output: false (obj1 and obj3 have different number of properties) Outputtrue falseDeep equality: Deep equality is a recursive shallow equality check.If the properties are objects, then the check is performed recursively on these objects.Example: In this example, the deepEqual function compares obj1 and obj2 deeply, including nested properties. Since both objects have the same property "a" with the same values, and nested property "c" with the same value, the function returns true for the comparison of obj1 and obj2 JavaScript function deepEqual(obj1, obj2) { if (obj1 === obj2) { return true; } if (typeof obj1 !== 'object' || obj1 === null || typeof obj2 !== 'object' || obj2 === null) { return false; } const keys1 = Object.keys(obj1); const keys2 = Object.keys(obj2); if (keys1.length !== keys2.length) { return false; } for (let key of keys1) { if (!obj2.hasOwnProperty(key) || !deepEqual(obj1[key], obj2[key])) { return false; } } return true; } const obj1 = { a: 1, b: { c: 2 } }; const obj2 = { a: 1, b: { c: 2 } }; const obj3 = { a: 1, b: { c: 3 } }; console.log(deepEqual(obj1, obj2)); // Output: true (Both objects have the same properties //and nested properties with the same values) console.log(deepEqual(obj1, obj3)); // Output: false (The nested property "c" //has different values in obj1 and obj3) Outputtrue false Comment More infoAdvertise with us Next Article What is object equality in JavaScript ? isitapol2002 Follow Improve Article Tags : JavaScript Web Technologies javascript-object JavaScript-Questions Similar Reads Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co 11 min read JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav 11 min read Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De 5 min read Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance 10 min read React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications 15+ min read Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact 12 min read React Tutorial React is a JavaScript Library known for front-end development (or user interface). It is popular due to its component-based architecture, Single Page Applications (SPAs), and Virtual DOM for building web applications that are fast, efficient, and scalable.Applications are built using reusable compon 8 min read JavaScript Interview Questions and Answers JavaScript (JS) is the most popular lightweight, scripting, and interpreted programming language. JavaScript is well-known as a scripting language for web pages, mobile apps, web servers, and many other platforms. Both front-end and back-end developers need to have a strong command of JavaScript, as 15+ min read Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and 9 min read Steady State Response In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We 9 min read Like