Sub topic 4
Arithmetic and Unary Operators
This chapter assumes you know JavaScript's primitive types and have read the previous chapter on type coercion, especially ToNumber. Here you will apply that conversion to the operators that do math.
The Arithmetic Operators
JavaScript has six binary arithmetic operators: +, -, *, /, %, and **. Every one except + always converts both operands with ToNumber before computing. The + operator has its own chapter next, because it can also join strings.
console.log('3' - 1); // 2
console.log('3' * '4'); // 12
console.log('10' / '4'); // 2.5
console.log(null * 3); // 0: null becomes 0
console.log(undefined - 1); // NaN: undefined becomes NaN
console.log('abc' * 2); // NaN: the string is not numeric
console.log(true - false); // 1Once a NaN enters a calculation, it carries through every later step. When a result comes back as NaN, find the first operand that failed to convert.
Division and Remainder
Division never throws. Dividing by zero gives Infinity or -Infinity, and 0 / 0 gives NaN. The % operator returns a remainder, not a mathematical modulo. Its result takes the sign of the left operand.
console.log(7 / 2); // 3.5: no integer division
console.log(5 / 0); // Infinity
console.log(-5 / 0); // -Infinity
console.log(0 / 0); // NaN
console.log(7 % 3); // 1
console.log(-7 % 3); // -1, not 2
console.log(((-7 % 3) + 3) % 3); // 2: a true modulo
console.log(2 ** -1); // 0.5Unary Operators
Unary operators take a single operand. Unary + applies ToNumber and does nothing else, so it is a short way to write Number(x). Unary - converts in the same way and then negates the result.
console.log(+' 42 '); // 42: whitespace is trimmed
console.log(+''); // 0
console.log(+'12px'); // NaN
console.log(parseInt('12px', 10)); // 12: parseInt reads leading digits
console.log(+[]); // 0: [] becomes '' and then 0
console.log(+[5]); // 5
console.log(+{}); // NaN
console.log(-'5'); // -5Increment and Decrement
++ and -- add or subtract 1 and store the result back in the variable. In prefix form (++x) the expression evaluates to the new value. In postfix form (x++) it evaluates to the old value.
let x = 5;
console.log(x++); // 5, and x is now 6
console.log(++x); // 7
let s = '9';
s++;
console.log(s, typeof s); // 10 'number'Common Errors
| Mistake | Result | Fix |
|---|---|---|
| `+'12px'` used to parse input | `NaN` | `parseInt(str, 10)` or validate first |
| `-7 % 3` expected to be 2 | `-1` | `((a % n) + n) % n` |
| `-2 ** 2` | SyntaxError | `(-2) ** 2` or `-(2 ** 2)` |
| `x++` inside an expression | Uses the old value | Increment on its own line |
Exercise
What does each line log? console.log('8' / '2', -'3' * 2, +[] - 1, '5' % 2, +undefined);
Show answer
4 -6 -1 1 NaN. Each operator converts its operands with ToNumber: '8' and '2' become 8 and 2, -'3' becomes -3, +[] becomes 0, '5' becomes 5, and undefined becomes NaN.
Exercise
Write a function isOdd(n) that returns true for 3, -3, and 7, and false for 0, 4, and -4.
Show answer
const isOdd = (n) => n % 2 !== 0; This works because the remainder for a negative odd number is -1. That value is not 0, so the check still returns true.