Implicit Type Coercion

Med

JS automatically converts types in operations. Understanding coercion prevents bugs. Use === to avoid comparison coercion.

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

  • Arithmetic converts to numbers
  • String + anything = string
  • == coerces, === does not
  • Logical ops return values
  • Falsy/truthy rules

Code Examples

Coercion Examples

"5" + 3; // "53" (string)
"5" - 3; // 2 (number)
"5" == 5; // true (coerced)
"5" === 5; // false (no coerce)

+ with string concatenates. Other ops convert to number.