Sub topic 12
Coercion in + and Other Arithmetic Operators
This chapter assumes you already know how ToPrimitive, ToNumber, and ToString work. Here you will see how arithmetic operators use those conversions. The + operator follows its own rule, and the other arithmetic operators share one simpler rule.
The + Operator Has Two Jobs
+ can add numbers or join strings. JavaScript decides which one to do by following these steps:
- Convert both operands with ToPrimitive using the
"default"hint. - If either primitive is a string, convert both to strings and concatenate them.
- Otherwise, convert both to numeric values (Number or BigInt) and add them.
- If one numeric value is a BigInt and the other is a Number, throw a
TypeError.
ToPrimitive runs first, so objects reach step 2 only after they have been turned into primitives. An array becomes a string through toString, which is why arrays usually push + toward concatenation.
1 + "2"; // "12": a string operand makes + concatenate
"3" - 1; // 2: - always converts to numbers
"3" * "4"; // 12
true + 1; // 2
1 + null; // 1
1 + undefined; // NaN
[] + []; // "": both arrays become ""
[] + {}; // "[object Object]"
1n + 2n; // 3n
// 1n + 1; // TypeError: Cannot mix BigInt and other typesIn true + 1 neither operand is a string, so true becomes 1. In 1 + null, null becomes 0. In 1 + undefined, undefined becomes NaN, and any arithmetic with NaN produces NaN.
Evaluation Order Matters
+ is evaluated left to right, so the point where a string first appears changes the result:
1 + 2 + "3"; // "33": 1 + 2 is 3, then 3 + "3"
"1" + 2 + 3; // "123": "1" + 2 is "12", then "12" + 3
"1" + (2 + 3); // "15": parentheses force the addition firstThe Other Arithmetic Operators
The operators -, *, /, %, and ** never concatenate. They convert both operands to numeric values, using the "number" hint for objects, and then do the math. The BigInt mixing rule still applies.
"10" / "4"; // 2.5
"5" % 2; // 1
[6] * [7]; // 42: each array becomes "6" and "7", then numbers
"abc" - 1; // NaN
2n ** 3n; // 8nUnary + applies ToNumber to its operand. That makes +"42" a short way to write Number("42"). Unary + on a BigInt throws a TypeError, because a BigInt converted to a Number could lose precision.
Common Errors
| Expression | Expected | Actual | Why |
|---|---|---|---|
| `"5" + 1` | `6` | `"51"` | A string operand makes `+` concatenate |
| `total + undefined` | `total` | `NaN` | `undefined` converts to `NaN` |
| `10n + 1` | `11n` | `TypeError` | BigInt and Number cannot be mixed |
| `[1, 2] + [3]` | `[1, 2, 3]` | `"1,23"` | Arrays become strings |
Exercise
Predict the results of "4" - 2 + "1" and null + "x" + 1.
Show answer
"4" - 2 is 2 because - converts both operands to numbers. Then 2 + "1" concatenates, giving "21". For the second expression, null + "x" has a string operand, so null becomes "null" and the result is "nullx". Then "nullx" + 1 gives "nullx1".
Exercise
Why does [] + {} produce "[object Object]" while [] - {} produces NaN?
Show answer
+ turns both operands into primitives: "" for the array and "[object Object]" for the object. One of them is a string, so + concatenates them. - converts both to numbers instead: "" becomes 0 and "[object Object]" becomes NaN, so the result is NaN.