Sub topic 3
Arithmetic Operators, ** and the Sign of %
This chapter assumes you know basic JavaScript syntax, can run code in a browser console or Node.js, and understand that every ordinary JavaScript number is a 64-bit floating-point value, as covered in the first chapter. It covers the arithmetic operators, how ** groups, and why % can return a negative result.
The Core Operators
JavaScript has seven arithmetic operators for Numbers. Division always produces a floating-point result. No operator does integer division.
| Operator | Meaning | Example | Result |
|---|---|---|---|
| `+` | Addition | `7 + 2` | `9` |
| `-` | Subtraction | `7 - 2` | `5` |
| `*` | Multiplication | `7 * 2` | `14` |
| `/` | Division | `7 / 2` | `3.5` |
| `%` | Remainder | `7 % 2` | `1` |
| `**` | Exponentiation | `7 ** 2` | `49` |
| `-x` | Negation | `-(7)` | `-7` |
Each operator has a compound assignment form (+=, -=, *=, /=, %=, **=). Each form updates a variable in place.
let total = 10;
total += 5; // 15
total *= 2; // 30
total **= 2; // 900
total %= 7; // 4
console.log(total); // 4
console.log(7 / 2); // 3.5, not 3
console.log(5.5 % 2); // 1.5, % works on fractions too
console.log(1 / 0); // Infinity, no error is thrownThe `+` Trap with Strings
The + operator concatenates when either operand is a string. The other arithmetic operators convert their operands to numbers. Mixing the two behaviors is a common source of bugs when values come from form fields.
console.log("3" + 4); // "34"
console.log("3" - 1); // 2
console.log("3" * "4"); // 12To avoid this, convert input to a number explicitly before you do arithmetic. A later chapter covers how to do that.
How `**` Groups
The ** operator is right-associative, which means a ** b ** c is evaluated as a ** (b ** c). JavaScript also refuses to guess what a unary minus directly before ** should mean, so you must add parentheses.
console.log(2 ** 3 ** 2); // 512, because 3 ** 2 = 9 is computed first
console.log((2 ** 3) ** 2); // 64
console.log((-2) ** 2); // 4
console.log(-(2 ** 2)); // -4
// console.log(-2 ** 2); // SyntaxErrorMath.pow(a, b) gives the same result as a ** b. Most modern code uses **.
The Sign of `%`
% is a remainder operator, not a mathematical modulo. Its result takes the sign of the dividend (the left operand) and ignores the sign of the divisor.
console.log(7 % 3); // 1
console.log(-7 % 3); // -1
console.log(7 % -3); // 1
console.log(-7 % -3); // -1
function mod(a, n) {
// Result is always in the range [0, n) for positive n
return ((a % n) + n) % n;
}
console.log(mod(-7, 3)); // 2
console.log(mod(7, 3)); // 1Common Errors
- Expecting
/to return an integer. Round the result explicitly instead. - Writing
-x ** 2and getting a SyntaxError. Add parentheses to show which you mean. - Using
%to wrap an index backward through an array, which produces a negative index. - Adding a string from user input with
+, which concatenates instead of adding.
Exercise
A carousel has 5 slides, numbered 0 to 4. When the user is on slide 0 and presses "previous", the code runs (index - 1) % 5. What does it return, and how do you fix it so it returns 4?
Show answer
(0 - 1) % 5 returns -1, because % takes the sign of the dividend. Use ((index - 1) % 5 + 5) % 5, which returns 4.
Exercise
What do 2 2 3 and (-3) 2 evaluate to, and why does -3 2 fail?
Show answer
2 2 3 is 2 8, which is 256, because is right-associative. (-3) 2 is 9. -3 2 is a SyntaxError because JavaScript does not allow a unary minus directly before ** without parentheses.