You will be able to use JavaScript arithmetic operators to add, subtract, multiply, divide, find remainders, raise numbers to powers, and change numeric values by one.
What it is
Arithmetic operators are symbols that perform math on numeric values. In JavaScript, the main operators are + for addition, - for subtraction, * for multiplication, / for division, % for modulus, ** for exponentiation, and ++ or -- for increment and decrement. They work on numbers, but JavaScript may convert other values to numbers first.
A useful mental model is that each operator takes one or two operands and returns a new value. Binary operators like + need two operands. Unary operators like ++ need one operand and can change that operand's value.
Why it matters
- Calculate totals, prices, scores, and measurements.
- Find remainders for pagination, clock math, and even/odd checks.
- Grow or shrink counters inside loops.
- Compute powers for compound interest, area, volume, or scaling.
- Convert user input into numeric results for programs.
Syntax or steps
The smallest useful pattern is to store a number, apply an operator, and store or print the result. Operators follow precedence: *, /, and % run before + and -. Use parentheses to control order.
let a = 10;
let b = 3;
let sum = a + b;
let difference = a - b;
let product = a * b;
let quotient = a / b;
let remainder = a % b;
let power = a ** b;
a++;
b += 2;
Use ++ when you need to add exactly one. Use += when you need to add a larger amount.
Example
const price = 10;
const quantity = 3;
const taxRate = 0.1;
const discount = 5;
const subtotal = price * quantity;
const tax = subtotal * taxRate;
const total = subtotal + tax - discount;
const average = subtotal / quantity;
console.log(subtotal); // 30
console.log(tax); // 3
console.log(total); // 28
console.log(average); // 10
console.log(10 % 3); // 1
console.log(2 ** 8); // 256
let count = 5;
console.log(count++); // 5
console.log(count); // 6
Part by part: price * quantity multiplies unit price by number of items. subtotal * taxRate calculates tax. subtotal + tax - discount adds tax and subtracts the discount. subtotal / quantity divides the subtotal by the quantity. 10 % 3 gives the remainder after dividing 10 by 3. 2 ** 8 raises 2 to the 8th power. count++ returns the old value first, then increases count by one.
Common mistakes
- Using
+with strings:"5" + 3becomes"53", not8. Convert withNumber("5")or use unary plus:+("5") + 3. - Assuming modulus is always positive:
-10 % 3is-1in JavaScript because the sign follows the left operand. - Confusing
x++and++x:x++uses the old value, then increments;++xincrements first, then uses the new value. - Dividing by zero:
1 / 0givesInfinity, and0 / 0givesNaN. Check inputs before dividing.
When to use it
Use arithmetic operators when you need direct numeric calculations. If you need repeated addition, multiplication, or scaling, use operators directly. If you need to change a loop counter by one, use ++. If you need to add a variable amount, use += instead of ++.
| Goal | Use | Example |
|---|---|---|
| Add two numbers | + | 5 + 3 |
| Find remainder | % | 10 % 3 |
| Raise to a power | ** | 2 ** 8 |
| Increase by one | ++ | count++ |
| Increase by amount | += | count += 5 |
Practice
Guided exercise: write code that calculates the area of a rectangle with width = 7 and height = 4, then increases the width by one and calculates the new area.
- Create variables for width and height.
- Use
*to calculate area. - Use
++to increase width. - Calculate the new area.
Expected output: 28 then 32.
Challenge: use modulus to determine whether 17 is even or odd. Hint: compare 17 % 2 to 0.
Quick check
Question: What does 10 % 3 return, and why?
Answer: It returns 1, because 3 goes into 10 three times with a remainder of 1.
Summary
Arithmetic operators let JavaScript perform basic math, calculate remainders, raise numbers to powers, and update counters. Choose the operator that matches the calculation, and remember that JavaScript can convert values and that increment operators have timing differences.