๐ŸŽญ JavaScript

JavaScript Type Coercion โ€” Why [] + {} Is Not What You Think

๐Ÿ“… Jul 4, 2026 โฑ 3 min read

JavaScript converts types silently โ€” the source of its famous "wat" moments and a guaranteed interview segment.

The rules that explain everything

The greatest hits

"5" + 2        // "52"    (+ prefers strings)
"5" - 2        // 3       (- forces numbers)
true + 1       // 2
[] + []        // ""      (arrays โ†’ empty strings)
[] + {}        // "[object Object]"
null == undefined   // true  (special pair)
null === undefined  // false
NaN === NaN    // false!  use Number.isNaN()
0.1 + 0.2 === 0.3   // false (floating point, not coercion)

Falsy โ€” memorize the six

false, 0, "", null, undefined, NaN. Everything else is truthy โ€” including "0", [] and {} (the trap: [] == false is true, but if ([]) runs!).

Defensive style: === always, explicit conversions (Number(x), String(x)), and ?? for defaults. Drill more in the JS question bank.

โ† All Articles