JavaScript · Question 6 of 50
What is the difference between == and ===?
What they're checking
Whether you know the coercion rules well enough to avoid them.
The answer
=== compares type and value with no conversion. == converts types first, by rules that surprise people: 0 == "" and null == undefined are true, "0" == false is true, but null == 0 is false.
Use === by default. The one common, deliberate use of == is x == null, which checks for null and undefined at once. NaN equals nothing, itself included: use Number.isNaN. Objects compare by reference, so two identical-looking objects are never ===.
0 == '' // true
'0' == false // true
null == undefined // true
null == 0 // false
NaN === NaN // false: use Number.isNaN(x)
[1] === [1] // false: two different arraysThe picture
a b == ===
0 '' true false
'0' false true false
null undefined true false
NaN NaN false false
{} {} false false (compared by reference)The follow-up they'll ask
How do you compare two objects by their contents?
Show the answer
Compare the fields you care about, or use a deep-equal helper. JSON.stringify only works when key order and values are simple.
Your progress stays on this device.