What is object equality in JavaScript ?
Last Updated :
21 Jul, 2023
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
=== 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
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
true
Shallow 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)
Deep 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)
Similar Reads
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 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
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 Check if Object is JSON in JavaScript ? JSON is used to store and exchange data in a structured format. To check if an object is JSON in JavaScript, you can use various approaches and methods. There are several possible approaches to check if the object is JSON in JavaScript which are as follows: Table of Content Using Constructor Type Ch
2 min read
Equality(==) Comparison Operator in JavaScript In JavaScript, the Comparison Equality Operator is used to compare the values of the operand. The comparison operator returns true only if the value of two operands are equal otherwise it returns false. It is also called a loose equality check as the operator performs a type conversion when comparin
2 min read