Lessons · JavaScript · why === and not ==
Why === and not ==
== converts the two sides to a common type before comparing, by rules almost nobody remembers; === compares as they are.
Hone is a place to practise programming. This is one of its lessons, written out in full and free to read without an account.
What it is for
'' == 0 is true and '1' == 1 is true. Code that relies on == passes tests by luck and fails on the input nobody tried.
How to think about it
Use === and !== everywhere. The one idiomatic exception is x == null, which catches null and undefined together on purpose.
Worked example
console.log(0 == "", null == undefined);true true: == converts before comparing.
console.log(0 === "", null === undefined);false false: === compares as-is.
console.log("1" == 1, "1" === 1);true false.console.log(NaN === NaN, Number.isNaN(NaN));false true: NaN is the one value not equal to itself, so === cannot find it.
Your turn
Is the status exactly paid?
if (status "paid")
Solve one with the tests running
The trap
=== has exactly one blind spot: NaN === NaN is false. Ask Number.isNaN(x) instead. Beyond that, when you see == in code you did not write, ask what type each side really has; the answer is often 'string, by accident'.