How Arithmetic Works in JavaScript (Examples Included)

In case you don’t know, arithmetic is just taking 2 or more numbers and adding, subtracting, multiplying, dividing, etc. with them.

It looks like so:

1 + 2 * 5

The answer is:


That + and * you see there are what JavaScript calls operators.

Here’s where some people might make the mistake of first adding 1 and 2, then multiplying it by 5. Which isn’t how arithmetic works.

The correct way is to first multiply 2 and 5 then add that to 1. This is due to a mathematical concept called the precedence of operators. It’s a rule.

There’s plenty of information and charts to help guide you onto what operator takes “precedence” over what. Just understand that * must go before +.

If you want to really control what goes before what and bypass the precedence rule, you can insert parentheses like this:

(1 + 2) * 5

These parentheses force the addition of 1 and 2 first, then multiplying that by 5 which gives us a totally different result:


Here’s another note to keep in mind, some operators have equal precedences like multiplication and division. Here, it doesn’t matter which one goes first, the results are the same.

Let’s do a little quiz.

Arithmetic Quiz

Example 1

What is the answer to:

32 + 4 * 3 - 3

The answer is:


Because of precedence, it looks more like 32 + (4 * 3) – 3. So it starts with the 4 multiplied by the middle 3. Then, the result of that is added to 32, and then finally 3 is subtracted from the overall.

What is the remainder or modulo operator?

This one’s special. The symbol for the remainder or modulo operator is %.

Its precedence is equal to * and /.

It produces the remainder of dividing 2 numbers.

Let’s test this:

What’s the remainder/modulo for these numbers?

Example 1

12 % 3

The answer is:


The reason for this being 0, is because once you divide 12 by 3, you are left with nothing else “remaining.”

Let’s take another example, this time with something remaining.

Example 2

12 % 5

The answer is:


Are you confused yet?

The reason it’s 2 is because that’s what is “remaining” once you remove two 5’s from 12 in the division process. You are left with a “remainder” of 2, not 0.4.

I guess you can think of it as, how many whole integers are left after the division operation?

Leave a Comment