Arithmetic Operators

In the previous lesson, we learned how JavaScript converts between types. Now let’s start working with operators, beginning with arithmetic operators that do math.

➕ What are Arithmetic Operators?

Arithmetic operators perform math on numbers, just like you do on a calculator. They take two values and produce a result.

Operator Meaning Example Result
+ Addition 5 + 2 7
- Subtraction 5 - 2 3
* Multiplication 5 * 2 10
/ Division 10 / 2 5
% Remainder (modulus) 7 % 2 1
** Exponentiation (power) 2 ** 3 8

Here they are in code:

arithmetic-operators.js
const price = 100;
const quantity = 3;
console.log(price + quantity); // 103
console.log(price - quantity); // 97
console.log(price * quantity); // 300
console.log(price / quantity); // 33.333...

Let’s walk through what each line does:

  • We store 100 in price and 3 in quantity, so we have two numbers to work with.
  • price + quantity adds them, giving 103.
  • price - quantity subtracts quantity from price, giving 97.
  • price * quantity multiplies them, giving 300.
  • price / quantity divides price by quantity, giving a long decimal 33.333... because the numbers don’t divide evenly.

➗ The Remainder Operator

The remainder operator % gives you what is left over after division. It is very useful for checking if a number is even or odd.

arithmetic-operators.js
console.log(10 % 2); // 0 → 10 divides evenly, so it is even
console.log(7 % 2); // 1 → 7 leaves a remainder, so it is odd

Let’s read each line:

  • 10 % 2 divides 10 by 2 with nothing left over, so the remainder is 0. A remainder of 0 means the number is even.
  • 7 % 2 divides 7 by 2 and leaves 1 behind, so the remainder is 1. A remainder that is not 0 means the number is odd.

A common use

number % 2 === 0 is the standard way to check if a number is even. If the remainder is 0, the number is even.

🔼 Increment and Decrement

The ++ operator adds one to a number, and -- subtracts one. They are often used in loops to count.

arithmetic-operators.js
let count = 5;
count++; // same as count = count + 1
console.log(count); // 6
count--; // same as count = count - 1
console.log(count); // 5

Let’s step through it:

  • We start with count set to 5.
  • count++ adds one to count, the same as writing count = count + 1, so count becomes 6.
  • count-- subtracts one from count, the same as writing count = count - 1, so count goes back to 5.

🧮 Operator Precedence

When you combine operators, JavaScript follows the same order as math: multiplication and division happen before addition and subtraction. Use parentheses to control the order yourself.

arithmetic-operators.js
console.log(2 + 3 * 4); // 14 → 3 * 4 happens first
console.log((2 + 3) * 4); // 20 → parentheses happen first

Let’s compare the two lines:

  • In 2 + 3 * 4, JavaScript runs 3 * 4 first (getting 12), then adds 2, giving 14.
  • In (2 + 3) * 4, the parentheses force 2 + 3 to run first (getting 5), then multiply by 4, giving 20.

When in doubt, use parentheses

Parentheses make your intent clear and prevent surprises. They are always calculated first.

⚠️ Common Mistakes to Avoid

Mistake Problem Solution
Adding numbers that are strings "5" + 2 gives "52", not 7 Convert with Number(...) first
Forgetting operator precedence The result is not what you expected Use parentheses to set the order
Dividing by zero JavaScript returns Infinity Check the divisor before dividing

🔧 Try It Yourself!

  1. Create two number variables and print the result of all six arithmetic operators on them.
  2. Use % to check whether a number is even or odd.
  3. Use ++ to increase a counter and print it.
  4. Predict the result of 2 + 3 * 4, then run it to check.

🧩 What You’ve Learned

  • ✅ Arithmetic operators do math: +, -, *, /, %, and **
  • ✅ The remainder operator % is useful for checking even or odd numbers
  • ++ adds one and -- subtracts one
  • ✅ Multiplication and division run before addition and subtraction
  • ✅ Parentheses control the order of operations

🚀 What’s Next?

Now that you can do math, we will learn how to compare values. Let’s continue to Comparison Operators.

Share & Connect