Learn about different types of equality checking in JavaScript.

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 inES2015Â .
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
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.


