Type Coercion

medium

Complete guide with examples and interview questions · 25 min

Definition

Say it out loud in an interview — clear, simple, no textbook wording.

If someone asks what type coercion is, I'd say: JavaScript converts values between types automatically when an operator needs it to. You never called Number() — the operator did it on your behalf.

The mental model that makes the weird stuff predictable: the operator decides the target type. + says "string if I see one, otherwise numbers." -, *, /, and < say "numbers, always." if, &&, and ! say "booleans." Once you know what the operator wants, the result stops being magic.

Interviewers probe the two places this bites: objects turning into primitives (valueOf/toString, hint order) and the falsy list. Nail those and every "trick question" is just arithmetic.

Quick Stats
25
Minutes
medium
Difficulty
5
Interview Q's

Key takeaways

  • Coercion is operator-driven: the operator picks a target type, then converts the operands
  • `+` concatenates if either side (after ToPrimitive) is a string; every other arithmetic operator converts to numbers
  • Objects become primitives via Symbol.toPrimitive first, then valueOf/toString in hint order — "string" hint tries toString first
  • Falsy is exactly 0, -0, 0n, "", null, undefined, NaN, false — everything else, including [] and {}, is truthy
  • Chained comparisons don't chain: 1 < 2 < 3 evaluates left to right as (1 < 2) < 3

Track your mastery

Sign in to save Q&A scores, coding challenge progress, points, and streaks across devices.

Sign in to save progress