Type coercion in arithmetic operations in JavaScript
The + operator is special
Wiki topics:
🌐 · Web Development

Type coercion in arithmetic operations in JavaScript
The + operator is special
+ means two different things depending on what's around it:
- If either side is a string → it joins them together (concatenation)
- If both sides are numbers → it adds them

console.log("1" + 1);
// "1" is a string, so JS turns the whole thing into string joining
// "1" + "1" → "11"
console.log(1 + "1");
// Same thing, order doesn't matter - one side is a string
// "1" + "1" → "11"
console.log(1 + 1);
// Both are actual numbers, so this is real math
// → 2
Think of it like glueing vs adding. If either piece is text, JS assumes you want to glue them together like sentences.
console.log("Hello" + " " + "World");
// All strings, so they just get glued end to end
// "Hello" + " " → "Hello "
// "Hello " + "World" → "Hello World"
The other operators (-, *, /, %) are NOT special
These operators only make sense for numbers — there’s no such thing as “subtracting” or “multiplying” text. So JavaScript always tries to convert strings to numbers first, no matter what.
console.log("5" - 2);
// "5" gets converted to the number 5
// 5 - 2 → 3
console.log("5" * 2);
// "5" becomes 5
// 5 * 2 → 10
console.log("5" / 2);
// "5" becomes 5
// 5 / 2 → 2.5
console.log("5" % 2);
// "5" becomes 5
// 5 % 2 (remainder of 5 divided by 2) → 1
Easy rule to remember
OperatorIf a string is involved+Glues strings together (or numbers, if both are strings)-, *, /, %Converts string to number, then does math
A fun trick people use to convert a string to a number is:

console.log("5" - 0);
// 5 (as a number now, not a string)
Because - forces numeric conversion, subtracting 0 doesn't change the value but turns it into a real number.
메타데이터
- post_id
- bb97d84e562c
- slug
- type-coercion-in-arithmetic-operations-in-javascript-bb97d84e562c
- url
- https://medium.com/@raviranjandav940/type-coercion-in-arithmetic-operations-in-javascript-bb97d84e562c
- canonical_url
- https://medium.com/@raviranjandav940/type-coercion-in-arithmetic-operations-in-javascript-bb97d84e562c
- author_url
- https://medium.com/@raviranjandav940
- status
- ok
- fetched_at
- 2026-08-25 20:46:58