Explain JavaScript's implicit coercion rules. What does 1 + '2' - 1 give?
Quick answer
`+` with a string concatenates; every other operator converts toward numbers. `1 + '2' - 1` → '12' then 12 - 1 → 11.
In detail
The `+` operator prefers strings: if either side is a string it concatenates. All other arithmetic/comparison operators coerce operands to numbers. So `1 + '2'` is '12', and `'12' - 1` coerces '12' to 12, giving 11.
11 + "2"; // "12"
2"6" - 1; // 5
3true + 1; // 2
4[] + []; // ""
5[] + {}; // "[object Object]"
61 + "2" - 1; // 11Why interviewers ask this: A staple output question that tests coercion depth.
Common follow-up questions
- →Why is [] + {} a string?
This is 1 of 118+ questions in the JavaScript Interview Kit
Get every question with detailed answers, follow-ups and real code — plus coding challenges and a last-minute revision sheet. One-time payment, instant access.
⚡ Get the JavaScript Interview Kit → ₹299