whatstheoutput_

#21: '5' + 3 vs '5' - 3

Topic: coercion · Difficulty: ★★

console.log('5' + 3);
console.log('5' - 3);

Output

53
2

Why

+ is overloaded: when either operand is a string, it means concatenation, so '5' + 3 converts 3 to '3' and glues them into '53'. Every other arithmetic operator, including -, has only one meaning — numeric subtraction — so it always converts both operands to numbers first. '5' becomes 5, and 5 - 3 is 2. This asymmetry between + and every other math operator is exactly why + is the operator that trips people up in mixed string/number code.

More coercion puzzles

Play today's puzzle →