What they're testing
Whether you know the language has sharp edges, and which tools you reach for around them.
The short answer~30 seconds
=== compares without coercion; == runs a convoluted coercion table first. NaN === NaN is false because IEEE 754 says NaN isn't equal to itself — use Number.isNaN() or Object.is() to test for it. The working convention: use === always, with one exception, x == null, which catches both null and undefined.
The long answer
The interesting part is that === isn't "intuitive equality" either. It's wrong for NaN as above, and it reports +0 === -0 as true even though the two are distinguishable — dividing by them yields Infinity and -Infinity. Object.is() exists to handle exactly those two cases, and it's the comparison React uses when deciding whether state changed.
What makes == dangerous isn't the coercion itself but that the relation isn't transitive: '' == 0 is true, '0' == 0 is true, and '' == '0' is false. A comparison where A equals B and B equals C but A doesn't equal C can't be reasoned about, so avoiding it is cheaper than memorising it.
NaN === NaN // false
Number.isNaN(NaN) // true
Object.is(NaN, NaN) // true
0 === -0 // true
Object.is(0, -0) // false
'' == 0 // true
'0' == 0 // true
'' == '0' // false <- không bắc cầu
null == undefined // true <- trường hợp duy nhất nên dùng ==
null === undefined // falseWhat they'll ask next
?isNaN() versus Number.isNaN()?
isNaN() coerces its argument first, so isNaN('abc') is true even though 'abc' isn't NaN. Number.isNaN() doesn't coerce and is true only for actual NaN. Always the latter.
These lose points
- Saying
=="compares values" and==="compares types too". It sounds fine and explains nothing about why'' != '0'.