So what’s the difference between ==
and ===
in Javascript? Nothing at all if the types on both ends are the same!
If they are different though, ==
will try to coerce, before compare and this is where a lot of confusion comes from.
42 == "42"; // true, "42" will be converted to number before comparison
1 == true; // true, because true will be converted to 1 before comparison
This also applies to relation comparisons like <
and even <=
.
There is no way to avoid coercion with relational comparisons. These are the only ones which are valid:
<
(less than)>
(greater than)<=
(less than or equal to)>=
(greater than or equal to)==
(equality with coercion)===
(strict equality, no coercion)