Equality of Arrays and Objects in JavaScript

Chidume Kenneth
3 min readDec 12, 2022

--

In JavaScript, the double equals sign(==) and triple equals sign(===) or strict equality are the two operators used in checking for the equality of two values. We remember that the double equals sign unlike the triple equals sign checks for only the equality of the two values but doesn’t care about if they are of the same type. For example:

// double equality
const num = 2
const num2 = '2'
console.log(2==2)

// output will be true

Looking at the above code snippet, we will find out that we are comparing a variable(num) of integer type to another variable(num2) of type string but it still outputted “true”. This shows that double equality doesn’t care about the type of the two operands been compared but rather if the values are the same.

// triple equality
const num = 2
const num2 = '2'
console.log(2===2)

// output will be false

Using triple equality sign on the same values outputs false because , triple equals sign or strict equality checks for both the equality of the values and the types as well. num is of type integer whereas num2 is of type string so they are not of the same type but the values they hold(2) are the same.

But when we apply either of this signs on an array or object the behaviour becomes strange. For example:

const obj1={
name: 'kenneth',
country:'Nigeria',
age:26
}
const obj2={
name: 'kenneth',
country:'Nigeria',
age:26
}
console.log(obj1 == obj2)
//this outputs false
console.log(obj1 === obj2)
// this still outputs false

The two objects even though they are the same (have the same key-value pairs) but it outputs false for both double equality sign and triple equality sign. WHY?

The reason is because objects and arrays variables don’t store the particular values assigned to them directly but rather they store a reference to the original place in memory where the array or object is stored. Take a look at the diagram below for proper understanding:

As we can see from the picture above, the variable “obj1” stores the reference of the memory where the real object is stored, which in this case is 12456754(I made up this number for explanation purpose) same with the variable “obj2” which also stores the reference of the memory where the real object is stored in memory which is 98537562. We can see that the value obj1 stores(12456754) is different from the value obj2 stores(98537562). It can be written properly like this:

const obj1 = 12456754;
const obj2 = 98537562;

So therefore in the real sense, comparing variables obj1 and obj2 with either double equality sign or triple equality sign gives false because the values they both hold (which is the reference to both objects in memory) are not the same, even though the properties(key and value) of the two objects are the same!.

The only way two objects or arrays variables can be the same is only if we equal or assign a new variable(obj2) to an array or object variable(obj1). For example:

const obj1={
name: 'kenneth',
country:'Nigeria',
age:26
}
const obj2 = obj1;

console.log(obj1 == obj2)
// this will output true
console.log(obj1 === obj2)
// this will still be true

So doing ‘const obj1 = obj2’ gives us two objects that are equal!. So therefore running both equality signs on them returns true.

NB: I used only objects in my examples but the same reason and explanation still applies to arrays.

--

--