Arithmetic Operators
The seven arithmetic operators cover every calculation you will do, and two of them behave in ways worth learning deliberately.
Plus, minus, times, divide, remainder and exponent, plus the increment and decrement shortcuts. Remainder is how you test for even numbers and cycle through a list; prefix and postfix increment differ in what they return.
EXERCISE
1The Six Calculations
Four of these behave exactly as you learned at school, and the remaining two are the ones that do real work in programs.
The four familiar ones:
console.log(10 + 3); // 13
console.log(10 - 3); // 7
console.log(10 * 3); // 30
console.log(10 / 3); // 3.3333333333333335
Remainder, written with a percent sign:
console.log(10 % 3); // 1 10 divided by 3 is 3, remainder 1
console.log(12 % 4); // 0 divides evenly
console.log(7 % 2); // 1 odd
console.log(8 % 2); // 0 even
// Which gives you the standard even/odd test:
const n = 14;
console.log(n % 2 === 0 ? "even" : "odd"); // even
Exponent, written with two stars:
console.log(2 ** 3); // 8 two cubed
console.log(5 ** 2); // 25
console.log(9 ** 0.5); // 3 a half power is a square root
Order of operations is the one from school:
console.log(2 + 3 * 4); // 14 not 20: multiply first
console.log((2 + 3) * 4); // 20 brackets win
console.log(10 - 4 - 3); // 3 left to right
console.log(2 ** 3 ** 2); // 512 exponent goes RIGHT to left
Pro Tip: Remainder is the operator beginners skip and professionals use constantly. Testing for even or odd, striping every third row of a table, wrapping an index back to the start of an array, converting seconds into minutes and seconds: all of them are
%. When you need something to cycle or repeat, it is almost always the answer.
EXERCISE
2Increment, Decrement, and Where They Differ
Adding one to a counter is so common that JavaScript has a two-character shortcut for it, with a subtlety in where you put it.
++ adds one, -- subtracts one. Both can sit before or after the variable, and the position changes what the expression gives back, not what the variable ends up holding.
On its own line, the position makes no difference:
let count = 5;
count++;
console.log(count); // 6
count--;
console.log(count); // 5
++count;
console.log(count); // 6 same result
Used inside a larger expression, it matters:
let a = 5;
console.log(a++); // 5 <- returns the OLD value, THEN adds
console.log(a); // 6
let b = 5;
console.log(++b); // 6 <- adds FIRST, then returns the new value
console.log(b); // 6
// Postfix: "use it, then increase it"
// Prefix: "increase it, then use it"
Where you will actually meet this:
const items = ["a", "b", "c"];
let i = 0;
console.log(items[i++]); // "a" reads index 0, then i becomes 1
console.log(items[i++]); // "b" reads index 1, then i becomes 2
console.log(i); // 2
// Clear once you know the rule, confusing if you do not.
Pro Tip: Write
count++on its own line and you never have to think about this. The moment you combine it with something else, you are asking a reader to remember which side the plus signs were on, which is a small cost to pay repeatedly. Most style guides say do the increment separately, and they are right.
EXERCISE
3Unary Minus, and When Plus Is Not Addition
The same symbols behave differently depending on how many values they sit between, which explains several results that look wrong at first.
An operator with two values is binary. With one value it is unary, and it can mean something else entirely.
Minus with one value flips the sign:
const debt = 250;
console.log(-debt); // -250
console.log(-(-5)); // 5
// And with one value, minus also converts to number:
console.log(-"5"); // -5
console.log(typeof -"5"); // number
Plus with one value converts, and does not add:
console.log(+"42"); // 42 string to number
console.log(+true); // 1
console.log(+false); // 0
console.log(+null); // 0
console.log(+"abc"); // NaN
// This is why you sometimes see +value in real code.
// It is Number(value) written in one character.
And the trap this creates with two values:
// Binary + joins if EITHER side is a string:
console.log(1 + 2 + "3"); // "33"
// left to right: 1 + 2 is 3, then 3 + "3" is "33"
console.log("1" + 2 + 3); // "123"
// left to right: "1" + 2 is "12", then "12" + 3 is "123"
// Every other operator converts to number instead:
console.log("6" / "2"); // 3
console.log("6" * "2"); // 12
When a sum comes out as text instead of a number, the cause is nearly always a value that arrived from an input, an attribute or an as a string. Convert it once when it arrives rather than the arithmetic further down, because + is the only operator that will not convert for you.
