Full answer
JavaScript has no type declarations, so operators must decide what to do when the two sides are of different types. Converting a value to the type an operation needs is called coercion. It is implicit when the engine does it for you, and explicit when you call Number(x), String(x), or Boolean(x) yourself.
The + operator has two jobs: addition and joining text. If either operand is a string after converting objects to primitives, the result is a string, so "5" + 3 becomes "53". The operators -, *, /, and % have only one job, arithmetic, so they convert both sides to numbers. "5" - 3 is 2.
Number conversion rules that appear often: an empty or whitespace-only string becomes 0; a string with letters becomes NaN; true becomes 1, false becomes 0; null becomes 0; undefined becomes NaN. String conversion of an array joins its items with commas, and an object becomes [object Object]. For objects, the engine calls Symbol.toPrimitive, then valueOf, then toString.
Unary + is a short way to convert to a number. parseInt and parseFloat read as much of a string as they can and ignore the rest, while Number is strict and returns NaN for any extra text. parseInt should be given a radix, as in parseInt(text, 10).
In practice, convert explicitly at the boundary where input arrives, such as form fields, which always give strings.
console.log('5' + 3, '5' - 3, '5' * '2');
console.log(1 + 2 + '3', '1' + 2 + 3);
console.log(Number(''), Number(' 42 '), Number('12px'), Number(null), Number(undefined), Number(true));
console.log(parseInt('12px', 10), parseFloat('3.5kg'), parseInt('abc', 10));
console.log(+'7', +'', -'3');
console.log(String([1, 2, 3]), String({}), String(null), String(undefined));
console.log([] + [], [] + {}, [1] * 2);
const price = {
valueOf() {
return 100;
},
toString() {
return 'hundred';
},
};
console.log(price + 1, `${price}`, price * 2);
const fieldValue = '25'; // form fields always give strings
console.log(fieldValue + 5, Number(fieldValue) + 5);Run with Node.js v24.22.1 as js-26.js; two runs gave identical output. Output: 53 2 10 33 123 0 42 NaN 0 NaN 1 12 3.5 NaN 7 0 -3 1,2,3 [object Object] null undefined [object Object] 2 101 hundred 200 255 30
Key points
- + joins text if either operand is a string; other arithmetic operators convert to numbers.
- Number("") is 0, Number("abc") is NaN, Number(null) is 0, Number(undefined) is NaN.
- Objects are converted using toPrimitive, valueOf, and toString.
- parseInt reads a leading number; Number rejects extra characters.
- Convert input explicitly rather than depending on implicit rules.
Common mistakes
- Adding two numbers read from a form field and getting joined text.
- Using parseInt without a radix or on values that are not strings, such as parseInt(0.0000005), which gives 5.
- Assuming Number("12px") returns 12.
- Trusting == and + with mixed types in business logic instead of converting explicitly.
Follow-ups
- What is the output of [] + {} and {} + [] in the console, and why does the second sometimes differ?
- How does Symbol.toPrimitive change coercion for an object?
- What is the difference between Number(x), parseInt(x), and unary plus?
Tips
- Explain with the one-sentence rule for + first, then the rule for other operators.
- Show an explicit conversion at an input boundary as the professional habit.
- If asked about [] + {}, do it step by step: both become strings, then join.