

Object Equality in JavaScript
source link: https://dev.to/suprabhasupi/object-equality-in-javascript-15ff
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.


Object Equality in JavaScript
Aug 14
・2 min read
It's really easy to compare number or strings but did you try comparing two objects 🤔
Even if two object has same key and value pair it will return false.
example:
let name = {
firstName: "suprabha",
lastName: "supi"
}
let fullName = {
firstName: "suprabha",
lastName: "supi"
}
console.log(name === name) // true
console.log(name === fullName) // false
console.log(name == fullName) // false
console.log(Object.is(name, fullName)) // false
console.log(Object.is(name, name)) // true
As you can see above example, both name
and fullName
are identical. Yet, the object are not equal either with ==
or ===
.
There are two things you can check while doing object equality:
1️⃣ Objects has same instance
2️⃣ Objects has same value
1️⃣ Objects has same instance
JavaScript has two approaches to match the values.
- For Primitive Type(string, numbers), it compare by their values.
- For Non-Primitive Type(object, array, date), it compare by their reference.
What does it mean by compare by their reference?
Comparing by reference means object refers to the same location in memory.
example:
let name = {
firstName: "suprabha",
lastName: "supi"
}
let fullName = {
firstName: "suprabha",
lastName: "supi"
}
let copyName = fullName
console.log(name === fullName) // false
console.log(copyName == fullName) // true
Here, copyName
and fullName
are referring to the same instance of memory and hence returning true.
2️⃣ Objects has same value
To check the instance you can use ===
equality, but to match the value you need to work more 😂
let name = {
firstName: "suprabha",
lastName: "supi"
}
let fullName = {
firstName: "suprabha",
lastName: "supi"
}
function isEqual(obj1, obj2) {
var props1 = Object.getOwnPropertyNames(obj1);
var props2 = Object.getOwnPropertyNames(obj2);
if (props1.length != props2.length) {
return false;
}
for (var i = 0; i < props1.length; i++) {
let val1 = obj1[props1[i]];
let val2 = obj2[props1[i]];
let isObjects = isObject(val1) && isObject(val2);
if (isObjects && !isEqual(val1, val2) || !isObjects && val1 !== val2) {
return false;
}
}
return true;
}
function isObject(object) {
return object != null && typeof object === 'object';
}
console.log(isEqual(name, fullName)); // true
There are few plugins which helps you in terms of the above condition where you can simply use _.isEqual
to check the object values:
let name = {
firstName: "suprabha",
lastName: "supi"
}
let fullName = {
firstName: "suprabha",
lastName: "supi"
}
console.log(_.isEqual(name, fullName)); // true
Summary ⅀
In object, if you do instance check for two objects then you can use ==
, ===
and Object.is()
.
However if you want to check for two object value, then you have to write your own logic to do it.
Thanks for reading the article ❤️
Recommend
-
42
If you're familiar with C#, then you most likely heard that you should always override Equals and GetHashCode for custom structs for performance reasons. To better understand the importance and...
-
71
-
37
README.md JavaScript Equality Table Game… …or why you should prefer the === operator.
-
48
There are two ways for testing the equality of class instances: reference equality and value equality. In this post we will discuss both of them. Hint: This post is using Swift 4 The Identity Operator =...
-
54
readme.md dequal
-
18
How an Optimizing Compiler Works The Scala language, following its Java heritage, allows you to compare any two values for equ...
-
24
Type equality in object oriented programming
-
13
Don't Make Javascript Equality Look Worse Than It Is 27 Mar 2014 Every now and then someone posts a table showing what values are equivalent according to javascript's == operator, like
-
10
Checking object equality with value objects In JavaScript, you can't check if two objects are equal like this: const point1 = { x: 2, y: 10 } const point2 = { y: 10, x: 2 } point1 === point2 // false
-
13
December 22, 2022 /
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK