JavaScript converts types silently โ the source of its famous "wat" moments and a guaranteed interview segment.
The rules that explain everything
- + with a string โ concatenation:
"5" + 2 โ "52" - Other math operators โ numbers:
"5" - 2 โ 3,"6" * "2" โ 12 - == converts before comparing; === never converts. Use ===
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.