whatstheoutput_

#7: null == undefined

Topic: coercion · Difficulty: ★★

console.log(null == undefined);
console.log(null === undefined);
console.log(null == 0);

Output

true
false
false

Why

The spec gives null and undefined a special rule: with loose ==, they equal each other and nothing else (except themselves). === never coerces, and they are different types, so that's false. The interesting trap is null == 0: you might expect null to act like 0 in comparisons since it does in some arithmetic contexts, but the == algorithm never converts null to a number — it only ever compares null/undefined loosely to each other, so null == 0 is false even though Number(null) is 0.

More coercion puzzles

Play today's puzzle →