Understand the Three Different Value Equality Comparison Algorithm in JavaScript.

Learn about different types of equality checking in JavaScript.

Image from Coffee Geek.

In JavaScript, there are three different value-comparison operations

  • Abstract Equality Comparison(loose equality || == || double equals).
  • Strict Equality(triple equals || === ).
  • Same Value Equality ( Object.is() ). Introduced in ES2015 .

Abstract Equality Comparison (==).

a == b;

Double Equals compares whether two values(a and b) are same .

During this comparison the type of the a and b is converted to same type.

This comparison doesn’t check for the type of the a and b .

If the a and b are different types , then we can use == comparison.

After converting , it will check if both the values are same

Example 1:

// here there is no type conversion
var a = 10;
var b = 10;
var c = 20;
a == b; // true
b == c; // false

Example 2 :

// with type conversion
var a = 10;
var b = '10';
var c = new String(10);
a == b; // true

In the above code a == b is evaluated to true , because the b is converted to number , and the comparison happens .

How type of the variables are converted is ,

Basic comparisons

Number and String .

a → number , b → string
b converted to number .
Example 
'1' == 1;  will be converted to  
Number(1) == 1 → 1 == 1 → true
--------

Examples

'' == 0;  // true because Number('') → 0
10' == 10.1; //false

A boolean and a non-boolean

a → boolean , b → non-boolean
a is converted to number, then comparison will be performed.
Example
'1' == true ;
this will changed as 
'1' == Number(true) → '1' == 1 
Now string and number comparison 
'1' == 1 → will be converted to 1 == 1 → true.

Examples

0 == false  // true
[] == false ; // true because Number([]) → 0 & Number(false) → 0
'' == false; // true because Number('') → 0 & Number(false) → 0
1 == true; // true

A string or a number to an object

a → string|number , b → object
b is converted to respective(a) primitive type and comparison will be performed
var a = '1';
var b = Number(1);
a == b;
Here , a → String & b → Number Object 
b is converted to String, 
'1' == 1 → '1' == String(1) → '1' == '1' → true

Examples

1 == String(1); // true
10 == Number(10); //true

Comparing NaN

NaN is not equivalent to anything – including NaN .

NaN == NaN; // false
NaN == 0; // false

Comparison with Infinity

Infinity|| -Infinity is truthy . But, when compared to true or false , it always return false .

Infinity == Infinity; // true
Infinity == - Infinity; // false
if(-Infinity)
console.log("will be printed")
if(Infinity)
console.log("will be printed");
Comparing with true and false.
Infinity == true ; // false.
Infinity == true → Number(Infinity) == Number(true) → Infinity == 1→ false.
Infinity == false; // false.

Comparing empty Array

if([])
console.log("This is printed");

An empty Array is true , because an Array is Object , an object without any properties is always true.

But

[] === true

When we compare empty array ([]) with boolean , When comparing boolean with non-boolean, both the values are converted to number and then comparison will takes place,

Number([]) == Number(true)
0 == 1; // false

Comparing Object

JavaScript, has two different approaches for testing equality. Primitives like strings and numbers are compared by their value, while objects like arrays, dates, and objects are compared by their reference. The reference are considered to be same if both the object points to same memory location.It is same for both triple equals and double equals

var a = {};
var b = {};
a == b; // false
a === b; // false

Concluding == comparison

// all true
false == 0;
0 == '';
null == undefined;
[] == false;
[0] == false;
[1] == true;
// all false
false == null;
NaN == NaN;
Infinity == true;
[] == true;
[0] == true;
[1] == false;

Strict Equality

a === b

Strict equality compares two values and types are equal .

If the type of two variables a and b is different , then a and b are not equal.

Example 1:

var a =10; 
var b =10;
a ===b; // true

Example 2 :

10 == '10'; // true
10 === '10'; //false

Example 3:

10 === Number('10'); // true 

Example 4 :

10 === new Number(10);//false 
because 
- 10 in Left side is Primitive
- 10 in Right side is Object
Both types are different 

Example 5:

var a = {};
var b = {};
a === b; // false , different reference
a === a; // true

In Strict Equality

* undefined === undefined; // true
* null === null; // true
* +0 === -0; // true
* NaN === NaN; // false
* Infinity === Infinity // fale
* Infinity === -Infinity // false
* null === undefined; // false

Object.is()

The Object.is() method determines whether two values are same.

In object is similar to triple equals comparison, except
Objects.is(+0, -0) // false
Object.is(NaN, NaN) // true
Object.is(NaN, 0/0);// true; Because 0/0 is NaN

Conclusion

Abstract Equality Algorithm

Strict Equality Comparison

Same value comparison

https://dorey.github.io/JavaScript-Equality-Table/

Thanks for Reading 📖 . Hope you like this. If you found any typo or mistake send me a private note 📝 thanks 🙏 😊 .

Follow me JavaScript Jeep🚙💨 .

Please make a donation here. 98% of your donation is donated to someone needs food 🥘. Thanks in advance.

Leave a comment

Design a site like this with WordPress.com
Get started