Arithmetic Operators

A computer is, at heart, a spectacularly fast calculator. Almost every program you will ever write — a game keeping score, a shop working out change, a phone counting down to midnight — leans on the same handful of everyday sums. In code, the little symbols that do those sums are called arithmetic operators, and the four you already know from maths lessons look almost exactly the same:

Each operator sits between two numbers — its operands — and produces a new number. Press Run and watch four sums come out at once:

console.log(3 + 4); // add → 7 console.log(10 - 6); // subtract → 4 console.log(5 * 6); // multiply → 30 console.log(20 / 4); // divide → 5

The comment after each // is just a note for humans — the computer ignores it. Try editing the numbers and running again; the machine never gets the arithmetic wrong (though, as we'll see, you can still tell it to do the wrong sum).

A real total: a shopping basket

Operators earn their keep when we stop typing bare numbers and start working with variables that stand for real quantities. Here we buy three pens and two notebooks and work out the bill. Change the prices or quantities and Run it again — the sum re-does itself instantly:

const penPrice: number = 120; // pence const bookPrice: number = 250; // pence const pens: number = 3; const books: number = 2; const total: number = pens * penPrice + books * bookPrice; console.log("Total in pence: " + total); console.log("Total in pounds: £" + total / 100);

Notice how the multiplications happen before the addition — pens * penPrice and books * bookPrice are each worked out first, and only then are the two totals added. That ordering isn't luck: it's a rule the computer follows every single time, and it's important enough to have its own name. More on that shortly.

Two extra operators: whole-number division and remainder

When you divide in maths you often get a quotient and a remainder: 17 \div 5 = 3 \text{ remainder } 2. Programmers use these two halves of a division constantly — think of sharing sweets into bags. So code gives us a way to get each one on its own:

Let's split a bag of sweets fairly between some friends and see exactly what's left over:

const sweets: number = 17; const friends: number = 5; const each: number = Math.floor(sweets / friends); // whole sweets per friend const leftOver: number = sweets % friends; // sweets that won't share evenly console.log("Each friend gets " + each + " sweets"); console.log(leftOver + " sweets are left over");

Together the two operators always tell the whole story of a division: 17 = 5 \times 3 + 2 — the Math.floor gives the 3, and % gives the 2. Change sweets to 20 and the remainder becomes 0: that's how you know a number divides exactly.

A favourite use: seconds into minutes and seconds

Here is % and Math.floor working as a team on a genuinely useful job: turning a stopwatch reading in seconds into "minutes and seconds", exactly like a timer on a screen. There are 60 seconds in a minute, so the number of whole minutes is the seconds divided by 60 and rounded down, and the seconds left on the clock are the remainder after % 60:

const totalSeconds: number = 200; const minutes: number = Math.floor(totalSeconds / 60); // whole minutes const seconds: number = totalSeconds % 60; // leftover seconds console.log(totalSeconds + " seconds = " + minutes + " min " + seconds + " sec");

200 seconds comes out as 3 minutes and 20 seconds — check it: 3 \times 60 + 20 = 200. The very same pattern turns pennies into pounds-and-pence, or a pile of eggs into full boxes of six plus a few spare. Whenever you meet "how many whole X, and how much left over?", reach for Math.floor and %.

The remainder operator has a couple of tricks that show up everywhere in programming:

Order matters: operator precedence

What is 2 + 3 \times 4? If you do the sum strictly left to right you get 20 — but the correct answer is 14, because multiplication is done before addition. This is the same order of operations ("BIDMAS" / "BODMAS") you use in maths, and computers obey it too. The rule, from strongest to weakest, is:

  1. Brackets ( ) — always worked out first;
  2. Multiply and divide (*, /, %) — next, left to right;
  3. Add and subtract (+, -) — last, left to right.

So % ranks alongside * and /. Run this to see precedence and brackets in action:

console.log(2 + 3 * 4); // × first → 2 + 12 = 14 console.log((2 + 3) * 4); // () first → 5 × 4 = 20 console.log(10 - 2 * 3); // × first → 10 - 6 = 4 console.log(20 / 2 / 5); // left to right → 10 / 5 = 2

Brackets are your override switch: whatever you put inside them is worked out first, no matter what. When you want the addition to happen before the multiply — as in (2 + 3) * 4 — brackets are how you say so.

The classic slip is assuming the computer works left to right. It doesn't — it follows precedence, so 2 + 3 * 4 is 14, not 20. If you meant "add first, then multiply", you must write the brackets yourself: (2 + 3) * 4.

When in doubt, add brackets. They cost nothing, and they make your intention obvious to the computer and to the next person reading your code — a program that says exactly what it means is easier to trust.

One more surprise waiting for you: in most programming languages / gives a decimal, not a whole number. 7 / 2 is 3.5, not 3. That's often exactly what you want — but when you need a whole-number answer (sweets per friend, whole minutes), reach for Math.floor(a / b) instead.