Skip to content
Theory · Operators and type conversion · lesson 1 of 14

Arithmetic

What you'll learn

What you'll learn:

  • compute a remainder and round with %, Math.floor and Math.trunc;
  • compare values with === and explain what makes == risky;
  • choose one of two values with &&, || and ??;
  • predict when JavaScript turns a string into a number, or a number into a string, on its own.
A heap of 135 minutes fills two boxes of 60, and 15 minutes are left outside

Arithmetic in a program is like a calculator with rules of its own. It adds, subtracts and divides as in school, but in a few ways it behaves differently. For example, it does not tell whole numbers from fractions.

Take a task that comes up all the time: how many hours are in 135 minutes? To turn minutes into hours, you divide them by 60. The result is 2.25, not 2: in JavaScript whole numbers and fractions share one type, number, so / keeps the fraction.

JavaScript
const minutes = 135;
console.log(minutes / 60); // 2.25
console.log(Math.floor(minutes / 60)); // 2
console.log(minutes % 60); // 15
console.log(2 ** 10); // 1024

Let's walk through the program line by line:

  1. minutes / 60 prints 2.25: two hours and another quarter of an hour.
  2. Math.floor(minutes / 60) drops the fraction and leaves 2, the number of whole hours.
  3. minutes % 60 prints 15: the minutes that did not fit into whole hours.
  4. 2 ** 10 prints 1024.

Remainder and power

Math.floor gives the whole part, and % gives the remainder of the division: 135 minutes are 2 hours and 15 minutes. The ** operator raises to a power: 2 ** 10 is the product of ten twos.

A handy way to picture the remainder: the minutes are packed into boxes, and each box holds exactly 60 minutes. Two boxes are full, and 15 minutes fit into none of them. Those 15 minutes are the remainder.

% divides fractions too: 5.5 % 2 is 1.5.

Order of operations

The order of operations is the same as in school:

  • first **;
  • then *, / and %;
  • + and - last.

Parentheses change this order: (2 + 3) * 4 gives 20.

Rounding

To understand rounding, picture a number line: smaller numbers on the left, larger ones on the right. The two ways of rounding behave differently on it:

  • Math.floor rounds down, toward the smaller number, so it always steps to the left;
  • Math.trunc drops the fraction and keeps only the digits before the dot.

For positive numbers both give the same result, but for negative ones they differ: Math.floor(-3.5) gives -4, while Math.trunc(-3.5) gives -3. The number -4 is smaller than -3, so "down" for Math.floor means further from zero.

The sign of the remainder

The remainder from % has one peculiarity: its sign matches the sign of the left number, the one being divided. In math a remainder is never negative, but in JavaScript it can be:

JavaScript
console.log(-7 % 3, 7 % -3); // -1 1
console.log(253 % 10); // 3

Here the box picture stops working: you cannot put negative minutes into a box, yet % works with them and takes its sign from the dividend.

Where the remainder comes in handy:

  • the remainder of a positive whole number divided by 10 is its last digit, like 3 for 253;
  • division by 2 tells even from odd: for an even number the remainder is 0, for an odd one 1 or -1.

Interviewers like to ask how to check whether a number is odd. The trap is the sign: -3 % 2 gives -1, not 1, so the check "the remainder equals 1" misses a negative odd number.

Changing a variable

To add a number to a variable, you do not have to write its name twice. total += 5 means the same as total = total + 5: as if five more coins went into the piggy bank total. This is compound assignment, and each operator has its own: -=, *=, /=, %=.

count++ adds one, and count-- subtracts one. Adding one is called an increment. All these forms change the variable, so it must be declared with let.

JavaScript
let total = 10;
total += 5;
total *= 2;
total++;
console.log(total); // 31

Step by step:

  1. let total = 10 puts the number 10 into total.
  2. total += 5 adds five: now it holds 15.
  3. total *= 2 multiplies by two: 30.
  4. total++ adds one, and the program prints 31.

Common mistake

The average of two numbers is computed without parentheses:

JavaScript
const a = 4;
const b = 6;
const average = a + b / 2; // ✗
console.log(average); // 7

Division runs before addition, so the program computes 4 + 3 and prints 7 instead of 5. JavaScript reports no error, and the wrong number travels on into later calculations. Put the sum in parentheses: (a + b) / 2 gives 5.

Key points

  • / keeps the fraction; Math.floor rounds down, and Math.trunc drops the fraction.
  • % gives the remainder, and its sign matches the dividend's: -7 % 3 is -1.
  • First **, then *, / and %, and + and - last; parentheses change the order.
  • += and the other compound assignments change a variable in place, and ++ adds one.

Subscribe to take the course

The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.

Sign in another way