Sub topic 4
Arithmetic and Unary Operators
This chapter assumes you can declare variables with let and const, know the JavaScript primitive types, and can run code in a browser console or Node.js. It also builds on the previous chapter's coercion rules, especially ToPrimitive and ToNumber.
The arithmetic operators are -, *, /, %, and **. The unary operators covered here are +, -, ++, and --. They share one rule: they always convert their operands to numbers. Binary + is the exception because it can also join strings, so it gets its own chapter next.
The Working Model: Convert, Then Compute
Every binary arithmetic operator follows the same steps. It evaluates the left operand, then the right operand. It converts each one with ToNumeric, which calls ToPrimitive with a "number" hint on objects and then ToNumber, or keeps a BigInt as a BigInt. Only after both conversions does it do the math.
The left-to-right order matters when a conversion has side effects:
const left = { valueOf() { console.log('left converted'); return 6; } };
const right = { valueOf() { console.log('right converted'); return 2; } };
console.log(left / right);
// left converted
// right converted
// 3Conversion in Practice
Since every operand goes through ToNumber, you can predict the result if you know how ToNumber treats each type:
console.log('6' / '2'); // 3
console.log('abc' * 2); // NaN
console.log(' 8 ' - 3); // 5: surrounding whitespace is trimmed
console.log(null * 5); // 0: null becomes 0
console.log(undefined * 5); // NaN: undefined becomes NaN
console.log(true - false); // 1
console.log([] * 2); // 0: [] -> '' -> 0
console.log([5] * 2); // 10: [5] -> '5' -> 5
console.log([1, 2] * 2); // NaN: [1, 2] -> '1,2' -> NaN
console.log({} - 1); // NaN: {} -> '[object Object]' -> NaN
const start = new Date('2026-01-01T00:00:00Z');
const end = new Date('2026-01-02T00:00:00Z');
console.log(end - start); // 86400000: Dates convert to milliseconds| Operand | ToNumber result |
|---|---|
| `''` or `' '` | `0` |
| `'42'`, `' 42 '` | `42` |
| `'0x1A'`, `'1e3'` | `26`, `1000` |
| `'12px'` | `NaN` |
| `null` | `0` |
| `undefined` | `NaN` |
| `true` / `false` | `1` / `0` |
| `[]` / `[7]` | `0` / `7` |
Division, Infinity, and Floating Point
JavaScript numbers are 64-bit IEEE 754 doubles. Division never throws:
console.log(1 / 0); // Infinity
console.log(-1 / 0); // -Infinity
console.log(0 / 0); // NaN
console.log(1 / -0); // -Infinity: the sign of zero is observable
console.log(7 / 2); // 3.5: there is no integer division
console.log(Math.trunc(7 / 2)); // 3
console.log(0.1 * 3); // 0.30000000000000004If you need integer division, wrap the result in Math.trunc (rounds toward zero) or Math.floor (rounds toward negative infinity). Those two only give different answers for negative numbers. For money, work in integer cents or use a decimal library. Don't rely on fractional doubles.
Remainder Is Not Modulo
The % operator returns a remainder. The result takes the sign of the left operand (the dividend). This catches people who expect mathematical modulo:
console.log(7 % 3); // 1
console.log(-7 % 3); // -1, not 2
console.log(7 % -3); // 1
console.log(5.5 % 2); // 1.5: works on fractions too
console.log(5 % 0); // NaN
function mod(n, m) {
return ((n % m) + m) % m;
}
console.log(mod(-7, 3)); // 2
console.log(mod(-1, 12)); // 11: useful for wrapping indexesA common bug is n % 2 === 1 as an odd-number test. It fails for negative odd numbers because -3 % 2 is -1. Use n % 2 !== 0 instead.
Exponentiation
The ** operator is right-associative (as covered in the precedence chapter), and a unary operator cannot appear directly before its left operand. Some results are less obvious:
console.log(2 ** -1); // 0.5
console.log(4 ** 0.5); // 2
console.log((-8) ** (1 / 3)); // NaN: fractional power of a negative
console.log(Math.cbrt(-8)); // -2
console.log(NaN ** 0); // 1: anything to the power 0 is 1
console.log('3' ** '2'); // 9Unary Plus and Unary Minus
Unary + is the shortest way to apply ToNumber explicitly. Unary - converts and then negates.
console.log(+'42'); // 42
console.log(+''); // 0
console.log(+'12px'); // NaN
console.log(parseInt('12px', 10)); // 12: parses a prefix instead
console.log(+true); // 1
console.log(-'3'); // -3
console.log(-null); // -0
console.log(Object.is(-null, -0)); // trueChoose +value or Number(value) when the whole string must be numeric. Choose parseInt or parseFloat when you want to read a leading number and ignore what follows. One more difference: Number(10n) returns 10, but +10n throws a TypeError, because unary + does not accept BigInt.
Increment and Decrement
++ and -- add or subtract 1 and assign the result back. The operand must be an assignable reference, such as a variable or property. The placement controls what the expression returns:
- Prefix (
++x) updates first and returns the new value. - Postfix (
x++) returns the old value, converted to a number, and then updates.
let a = 5;
const b = a++; // b is 5, a is 6
const c = ++a; // a is 7, c is 7
console.log(a, b, c); // 7 5 7
let s = '5';
const old = s++;
console.log(old, typeof old); // 5 'number': the old value is converted too
console.log(s); // 6
const obj = { n: 1 };
obj.n++;
console.log(obj.n); // 2
const fixed = 1;
try {
fixed++;
} catch (e) {
console.log(e instanceof TypeError); // true: cannot assign to a const
}
// 5++ and ++(a + 1) are SyntaxErrors: the operand is not assignableOther Unary Operators, Briefly
| Operator | Converts operand with | Covered in |
|---|---|---|
| `!` | ToBoolean | Logical operators |
| `~` | ToInt32 | Bitwise operators |
| `typeof` | no conversion | Type operators |
| `void` | none; always returns `undefined` | here: `void 0 === undefined` |
| `delete` | none; removes a property | object topics |
Common Errors Checklist
- Assuming
%behaves like mathematical modulo for negative numbers. - Reading form inputs (always strings) and doing math without validating them, which lets NaN through.
- Expecting
nullandundefinedto behave the same in arithmetic.nullbecomes0, andundefinedbecomesNaN. - Using
+strwhen the input may have units or trailing text. - Depending on postfix versus prefix in complex expressions.
Exercise
What do these log, and why? console.log(null + 1, undefined - 1, '10' / [2], -7 % 4);
Show answer
1 NaN 5 -3. null converts to 0, so 0 + 1 is 1. undefined converts to NaN, and NaN - 1 is NaN. [2] converts to '2' and then to 2, so 10 / 2 is 5. The remainder takes the dividend's sign, so -7 % 4 is -3.
Exercise
After let x = '3'; const y = x-- + 1;, what are x, y, and typeof x?
Show answer
x is 2, y is 4, and typeof x is 'number'. Postfix -- converts '3' to the number 3, returns 3, and stores 2 in x. Then 3 + 1 is 4. Both operands are numbers by that point, so + adds them.
Exercise
Write a function wrapIndex(i, length) that maps any integer, including negative ones, into the range 0 to length - 1.
Show answer
function wrapIndex(i, length) { return ((i % length) + length) % length; }. The inner % can return a negative remainder. Adding length makes it non-negative, and the outer % brings values at or above length back into range. For example, wrapIndex(-1, 5) is 4 and wrapIndex(7, 5) is 2.