Notorious Coercion Edge Cases

Hard

Famous JS quirks: [] + [], typeof null, NaN !== NaN. Know these for interviews and to write defensive code.

Interactive Visualization

Type Coercion Lab

Pick values and operators to see how JavaScript converts types.

Left value
Operator
Right value
Result
7=="7"=true
numberstringboolean
What JS Does
  • Number == String: string is coerced to number
  • "7" → Number("7") = 7
  • Then 7 == 7 is compared

Key Points

  • [] + [] = ""
  • [] + {} = "[object Object]"
  • typeof null = "object"
  • NaN !== NaN

Code Examples

Famous Gotchas

[] + []; // ""
[] + {}; // "[object Object]"
typeof null; // "object" (bug)
NaN === NaN; // false

Historic bugs and quirks in JS coercion