Arithmetic Operators
Table of Contents
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:
const price = 100;const quantity = 3;
console.log(price + quantity); // 103console.log(price - quantity); // 97console.log(price * quantity); // 300console.log(price / quantity); // 33.333...Let’s walk through what each line does:
- We store
100inpriceand3inquantity, so we have two numbers to work with. price + quantityadds them, giving103.price - quantitysubtractsquantityfromprice, giving97.price * quantitymultiplies them, giving300.price / quantitydividespricebyquantity, giving a long decimal33.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.
console.log(10 % 2); // 0 → 10 divides evenly, so it is evenconsole.log(7 % 2); // 1 → 7 leaves a remainder, so it is oddLet’s read each line:
10 % 2divides10by2with nothing left over, so the remainder is0. A remainder of0means the number is even.7 % 2divides7by2and leaves1behind, so the remainder is1. A remainder that is not0means 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.
let count = 5;
count++; // same as count = count + 1console.log(count); // 6
count--; // same as count = count - 1console.log(count); // 5Let’s step through it:
- We start with
countset to5. count++adds one tocount, the same as writingcount = count + 1, socountbecomes6.count--subtracts one fromcount, the same as writingcount = count - 1, socountgoes back to5.
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.
console.log(2 + 3 * 4); // 14 → 3 * 4 happens firstconsole.log((2 + 3) * 4); // 20 → parentheses happen firstLet’s compare the two lines:
- In
2 + 3 * 4, JavaScript runs3 * 4first (getting12), then adds2, giving14. - In
(2 + 3) * 4, the parentheses force2 + 3to run first (getting5), then multiply by4, giving20.
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!
- Create two number variables and print the result of all six arithmetic operators on them.
- Use
%to check whether a number is even or odd. - Use
++to increase a counter and print it. - 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.