Arithmetic and Assignment Operators
An operator is a symbol that performs an operation on one or more values, called operands. In fee * 2, the operator is * and the operands are fee and 2. Arithmetic operators calculate with numbers; assignment operators store the result in a variable. The Fee Tracker uses both on every line that works out a balance, a late fee or a discount.
In One Minute
- Arithmetic:
+-*/%(remainder)**(power). ++adds 1 and--subtracts 1. Prefercount += 1in most code, because it reads clearly.- Compound assignment:
total += feemeanstotal = total + fee. The same works for-=,*=,/=,%=and**=. +joins strings:"120" + 30is"12030". The other arithmetic operators convert strings to numbers.- Division never throws:
10 / 0isInfinity, and invalid math givesNaN.
Arithmetic Operators
| Operator | Name | Example | Result |
|---|---|---|---|
+ | Addition | 120 + 30 | 150 |
- | Subtraction | 120 - 30 | 90 |
* | Multiplication | 120 * 3 | 360 |
/ | Division | 120 / 8 | 15 |
% | Remainder | 125 % 10 | 5 |
** | Exponentiation (power) | 2 ** 10 | 1024 |
-x | Negation | -fee | the opposite sign |
const termFee = 450;
const terms = 3;
console.log("Year:", termFee * terms);
console.log("Per month:", termFee * terms / 12);
console.log("Whole months of $100:", Math.floor(termFee / 100));
console.log("Left over:", termFee % 100);
console.log("2 to the 8th:", 2 ** 8);Year: 1350
Per month: 112.5
Whole months of $100: 4
Left over: 50
2 to the 8th: 256Explanation: JavaScript has one number type for whole numbers and decimals, so 1350 / 12 gives 112.5 (not 112 as integer division would in Java). % gives what is left after dividing: 450 is four 100s with 50 left over.
The Remainder Operator
% is more useful than it first looks:
| Task | Expression |
|---|---|
Is n even? | n % 2 === 0 |
| Every third row | row % 3 === 0 |
| Wrap around a list of 7 days | (day + 1) % 7 |
| Cents part of an amount in cents | cents % 100 |
The result takes the sign of the left operand: -7 % 3 is -1. That is why it is called the remainder, not the mathematical modulo.
Increment and Decrement
++ adds 1 to a variable and -- subtracts 1. Each has two forms, and they differ in the value the expression produces:
let count = 5;
console.log(count++); // postfix: gives the old value, then adds 1
console.log(count);
console.log(++count); // prefix: adds 1, then gives the new value
console.log(count);5
6
7
7Assignment Operators
= stores a value. Compound assignment operators combine an operation with storing:
| Operator | Example | Same as |
|---|---|---|
= | x = 5 | |
+= | x += 5 | x = x + 5 |
-= | x -= 5 | x = x - 5 |
*= | x *= 5 | x = x * 5 |
/= | x /= 5 | x = x / 5 |
%= | x %= 5 | x = x % 5 |
**= | x **= 2 | x = x ** 2 |
There are also logical assignment operators (||=, &&=, ??=), covered in the logical operators lesson.
let balance = 450;
balance -= 200; // first payment
balance -= 150; // second payment
balance *= 1.05; // 5% late charge on what is left
console.log(balance);105+ with Strings
The + operator does two jobs: it adds numbers and joins (concatenates) strings. If either operand is a string, it joins:
console.log(120 + 30);
console.log("120" + 30);
console.log(120 + "30");
console.log(1 + 2 + "3");
console.log("1" + 2 + 3);
console.log("120" - 30);
console.log("120" * 2);150
12030
12030
33
123
90
240Explanation: operators run left to right. In 1 + 2 + "3", 1 + 2 is 3 first, then 3 + "3" joins to "33". In "1" + 2 + 3, the first step already makes a string, so everything after joins. The other operators (-, *, /) only work on numbers, so they convert strings to numbers: "120" - 30 is 90.
Division by Zero and NaN
JavaScript arithmetic never throws an error. Instead, it produces special number values:
console.log(120 / 0);
console.log(-120 / 0);
console.log(0 / 0);
console.log("abc" * 2);
console.log(Math.sqrt(-1));Infinity
-Infinity
NaN
NaN
NaNNaN means "Not a Number": the result of math that has no meaningful number answer. It spreads: any arithmetic with NaN gives NaN. The numbers lesson shows how to test for it (Number.isNaN).
In Practice
The Fee Tracker works out a late fee: $2 per day late, capped at $30, and adds it to the balance. Amounts are kept in cents (whole numbers) so decimals don't cause rounding errors; the numbers chapter explains why.
const balanceCents = 12000; // $120.00
const daysLate = 18;
const ratePerDayCents = 200; // $2.00
const capCents = 3000; // $30.00
let lateFeeCents = daysLate * ratePerDayCents;
if (lateFeeCents > capCents) {
lateFeeCents = capCents;
}
const totalCents = balanceCents + lateFeeCents;
console.log("Late fee: $" + lateFeeCents / 100);
console.log("Total due: $" + totalCents / 100);Late fee: $30
Total due: $150Common Mistakes
Adding numbers that are really strings. "50" + "25" is "5025". Convert with Number() first.
Expecting integer division. 7 / 2 is 3.5. Use Math.floor(7 / 2) (or Math.trunc for negative numbers) when you need a whole number.
Writing x =+ 5. That is x = +5 (assign positive 5), not x += 5. Read compound operators carefully.
Using ^ for power. 2 ^ 3 is 1 (a bitwise operator). Power is 2 ** 3, which is 8.
Practice
Challenge: A family pays $1,350 a year in 12 equal monthly payments, rounded down to whole dollars, with the remainder added to the last month. Print the regular payment and the last payment.
Show solution
const yearly = 1350;
const months = 12;
const regular = Math.floor(yearly / months);
const last = regular + (yearly % months);
console.log("Regular payment: $" + regular);
console.log("Last payment: $" + last);Regular payment: $112
Last payment: $118Key Takeaways
| Operator | Use |
|---|---|
% | Remainder: even/odd, wrap-around, leftovers |
** | Power: 2 ** 10 |
+= | Add to a running total |
Number(x) | Convert text to a number before adding |
Was this tutorial helpful?