=== (strict equality) compares value and type with no conversion. == (loose equality) performs type coercion first, which leads to surprising results.
js
== ;
== ;
== ;
== ;
== ;
=== ;
=== ;
=== (strict equality) compares value and type with no conversion. == (loose equality) performs type coercion first, which leads to surprising results.
== ;
== ;
== ;
== ;
== ;
=== ;
=== ;
The coercion rules are non-obvious and lead to bugs. For example, [] == ![] is true (a notorious quirk). You rarely want JavaScript silently converting types behind your back during a comparison.
Always use === (and !==). The one common, intentional exception is checking for null or undefined together:
if (value == null) { ... } // true for BOTH null and undefined — a deliberate idiom
For everything else, strict equality keeps comparisons predictable. To compare objects you need a deep-equality check (they compare by reference, so {a:1} === {a:1} is false).