Arithmetic operators in JavaScript
Beginner · 4 min read
JavaScript can add, subtract, multiply and divide, just like a calculator. It also has an operator for remainders and one for powers. These six symbols are behind every shopping total, progress bar and game score you will ever write.
In this tutorial you will take two numbers, 17 and 5, and run them through every operator. Then you will work out what 2 + 3 * 4 ** 2 gives before running it. That second part is about the order JavaScript works things out in, and it catches a lot of people.
You only need to know how to store a value with const and print it with console.log.
The six operators
Here they are, each with a tiny example:
console.log(7 + 2); // 9: addition
console.log(7 - 2); // 5: subtraction
console.log(7 * 2); // 14: multiplication
console.log(7 / 2); // 3.5: division
console.log(7 % 2); // 1: remainder
console.log(7 ** 2); // 49: power, 7 times 7
Two of these surprise beginners. First, / keeps the fraction: 7 / 2 is 3.5, not 3. Some languages throw the fraction away when both numbers are whole. JavaScript never does.
Second, % is not a percentage. It is the remainder: what is left over after dividing. 2 goes into 7 three times, which uses up 6, and 1 is left.
Which operator goes first
When a line has several operators, JavaScript doesn't simply read left to right. It follows an order called precedence, the same idea as the order of operations from school math:
**first.- Then
*,/and%. - Then
+and-.
Brackets beat all of them. Whatever is inside ( ) is worked out first.
Here is the exercise. Try it on your own first if you like, then read on and we will build it together.
1 The seven arithmetic operators
easyTake 17 and 5 and produce their sum, difference, product, quotient, remainder and power. Then work out what 2 + 3 * 4 ** 2 is without running it.
Stuck? Three hints, smallest first
- 1. There are six binary operators to reach for:
+ - * / % **. - 2.
**binds tighter than*, which binds tighter than+. - 3.
**is the one operator here that associates to the right.
Step 1: the first three
Store the two numbers once, so every line uses the same values.
const a = 17;
const b = 5;
const sum = a + b;
const difference = a - b;
const product = a * b;
console.log(sum, difference, product); // 22 12 85
Nothing tricky yet. Giving each result a name (sum, difference, product) makes the later lines easy to read.
Step 2: division and remainder
const a = 17;
const b = 5;
const quotient = a / b;
const remainder = a % b;
console.log(quotient, remainder); // 3.4 2
17 / 5 is 3.4. If you wanted 3, you have to ask for it. Math.trunc drops the fraction:
console.log(Math.trunc(17 / 5)); // 3
The remainder is the part that whole-number division threw away. Five goes into 17 three times (that's 15), and 2 is left. You can prove the two pieces fit together, because three fives plus the remainder rebuild 17:
console.log(5 * Math.trunc(17 / 5) + 17 % 5); // 17
Step 3: the power operator
a ** 2 means "a to the power of 2", which is a * a.
const a = 17;
const power = a ** 2;
console.log(power); // 289
Step 4: work out 2 + 3 * 4 ** 2
Now the puzzle. Reading left to right, you might say: 2 + 3 is 5, times 4 is 20, squared is 400. That is the natural wrong turn. Follow the precedence list instead:
4 ** 2is 16.3 * 16is 48.2 + 48is 50.
const mixed = 2 + 3 * 4 ** 2;
const bracketed = (2 + 3) * 4 ** 2;
console.log(mixed); // 50
console.log(bracketed); // 80
Brackets around 2 + 3 change the answer to 80, because now the addition happens first and you get 5 times 16.
Step 5: ** groups from the right
Most operators group from the left. 10 - 3 - 2 means (10 - 3) - 2, which is 5. The power operator is the odd one out: it groups from the right, the same way stacked powers work in written math.
const tower = 2 ** 3 ** 2; // 2 ** 9
const grouped = (2 ** 3) ** 2; // 8 ** 2
console.log(tower, grouped); // 512 64
2 ** 3 ** 2 means 2 ** (3 ** 2), which is 2 to the 9th, or 512. If you want 64, write the brackets yourself.
The full solution
The answer panel puts every step together and prints each result with a label. The checks underneath confirm each value, including that the quotient and remainder rebuild 17. Press Run to see the output.
const a = 17;
const b = 5;
const sum = a + b;
const difference = a - b;
const product = a * b;
const quotient = a / b; // 3.4 — division never truncates in JavaScript
const remainder = a % b; // what is left after 3 whole fives
const power = a ** 2;
// Precedence: ** first, then * / %, then + -.
const mixed = 2 + 3 * 4 ** 2; // 4**2 = 16, 3*16 = 48, 48+2 = 50
const bracketed = (2 + 3) * 4 ** 2; // 5*16 = 80
// ** associates to the right; everything else here goes left to right.
const tower = 2 ** 3 ** 2; // 2 ** 9
const grouped = (2 ** 3) ** 2; // 8 ** 2
console.log('sum, difference, product :', sum, difference, product);
console.log('quotient, remainder :', quotient, remainder);
console.log('17 squared :', power);
console.log('2 + 3 * 4 ** 2 :', mixed);
console.log('(2 + 3) * 4 ** 2 :', bracketed);
console.log('2 ** 3 ** 2 :', tower, 'not', grouped);
Output
9 checks that prove this answer
You do not have to read these. They are part of the answer panel above, so pressing
Run there executes all 9 of them and prints ok for each.
That output is the proof, and the build fails if a single check fails.
test('addition', sum, 22);
test('subtraction', difference, 12);
test('multiplication', product, 85);
test('division keeps the fraction, unlike C or Java', quotient, 3.4);
test('the remainder is what division threw away', remainder, 2);
test('and the two rebuild the original', b * Math.trunc(quotient) + remainder, 17);
test('** binds tighter than *', mixed, 50);
test('brackets override that', bracketed, 80);
test('** associates to the right, so it is 2 ** 9', [tower, grouped], [512, 64]);
Two habits come out of this. First, / is real division: 17 / 5 is 3.4, and if you wanted 3 you have to say so with Math.trunc or Math.floor. Coming from a language where integer division truncates, this is the first thing that bites.
Second, ** is the odd one out on associativity. 2 ** 3 ** 2 is 512, not 64, because it groups from the right the way the written notation does. Related trap: -2 ** 2 is a syntax error rather than -4 — the language refuses to guess, so write -(2 ** 2) or (-2) ** 2.
Common mistakes
- Expecting
/to drop the fraction.17 / 5is3.4. UseMath.truncorMath.floorwhen you need a whole number. - Reading
%as "percent".17 % 5is the remainder,2. To take 5% of a number, multiply by0.05. - Writing
-2 ** 2. JavaScript won't guess whether you mean-(2 ** 2)or(-2) ** 2, so the line is aSyntaxError. Add brackets to say which one you mean.
Try it yourself
- Use
%to check whether 2024 divides evenly by 4. What result means "yes"? - Without running them, work out
10 - 4 - 3and2 ** 2 ** 3. Then run them and check. - You have 47 eggs and boxes that hold 12. Use
Math.truncand%to print how many boxes you fill and how many eggs are left over.