Skip to content

Type Coercion

Type coercion is JavaScript’s implicit type conversion — when JavaScript automatically converts a value to another type during operations.

Coercion happens everywhere in JavaScript. Understanding it is essential to avoid bugs that seem like magic.

Coercion is like a restaurant that adapts your order. You ask for “2” beers (string), and the waiter brings 2 beers (number). It’s convenient, but sometimes you get something unexpected.

flowchart TD
A["'5' - 3"] --> B["2 (number)"]
C["'5' + 3"] --> D["'53' (string)"]
E["'5' * '2'"] --> F["10 (number)"]
G["!'hello'"] --> H["false (boolean)"]
// String coercion with +
'5' + 3; // '53'
'5' + true; // '5true'
'5' + null; // '5null'
// Number coercion with -, *, /
'5' - 3; // 2
'10' * '2'; // 20
'10' / '2'; // 5
// Boolean coercion
if ('hello') {} // truthy
if (0) {} // falsy
!'hello'; // false
!!'hello'; // true
5 == '5'; // true
0 == false; // true
'' == false; // true
null == undefined; // true
  • Use === and !== instead of == and !=
  • Be explicit with conversions — don’t rely on coercion
  • Use Number.isNaN() instead of global isNaN()
[] == false; // true — surprising!
[] == ![]; // true — the famous JavaScript wtf
  • Coercion is implicit type conversion
  • + prefers string concatenation
  • -, *, / prefer numeric conversion
  • Always use === to avoid coercion surprises