When inquiring about the dissimilarity between == and ===, most individuals will assert that == performs loose type checking, while === involves both type and value checking.
This doesn't capture the entire truth.
This is utterly incorrect. ❌
==, or abstract equality, performs type checking as its primary function. In fact, the initial action of == is to conduct type-checking.
Let's discover together.
== (Abstract Equality) performs four distinct actions when comparing two variables, a
and b
.
1 == 1;
// Internally return 1 === 1
If a
is null and b
is undefined, return true
If a
is undefined and b
is null, return true
null == undefined; // true
undefined == null; // true
[] == ""; // [ ] will be converted into a string
[].toString() == "";
"" == ""; // true
If the variable a
is of the Number type, convert variable b
to Number by encapsulating it within a Number function call, and subsequently compare the outcomes.
Conversely, if the variable b
is of the Number type, convert variable a
to Number by encapsulating it within a Number function call, and then compare the results.
"1" == 1; // "1" will be converted into Number
Number("1") == 1
1 == 1 // true
Observing the steps above leads to the conclusion that ==(Abstract Equality) involves type coercion during variable comparison, whereas ===(Strict Equality) does not perform any type coercion.
This constitutes the fundamental distinction between == and ===.
Happy Coding! 🚀