4 / 12 · Intro
Numbers and math
In JavaScript 0.1 + 0.2 gives 0.30000000000000004, and 17 % 5 gives 2. Both make sense once you know how numbers are stored and what % means.
Jump to the 12 practice questionsYou will learn
- Calculate with + - * / % ** in the right order
- Update a variable with +=, ++ and their partners
- Use Math helpers and show decimals with toFixed
Before this: Variables: let and const
1Explain
The arithmetic operators
Multiplication uses an asterisk, because the letter x would be confused with a variable called x. Division keeps the fractional part: 7 / 2 is 3.5, not 3. Some results are slightly rounded, because numbers are stored in binary. The remainder operator % divides and keeps only what is left over. 17 % 5 is 2, because 5 goes into 17 three times (15) with 2 left.
| Operator | Meaning | Example | Result |
|---|---|---|---|
| + | add | 7 + 2 | 9 |
| - | subtract | 7 - 2 | 5 |
| * | multiply | 7 * 2 | 14 |
| / | divide | 7 / 2 | 3.5 |
| % | remainder | 7 % 2 | 1 |
| ** | power | 7 ** 2 | 49 |
2Explain
Order of operations
JavaScript follows the same order as school maths. Powers come first, then multiplication, division and remainder, then addition and subtraction. Operators of equal rank are worked from left to right, except ** which works from right to left. Parentheses override everything, so when in doubt, add them; they cost nothing and make the intent clear to the next reader.
console.log(2 + 3 * 4); // 14
console.log((2 + 3) * 4); // 20
console.log(20 - 6 / 2); // 17
console.log(2 ** 3 * 5); // 4014
20
17
403Problem
Which part goes first?
What does this print?
console.log(2 + 3 * 4);Hint
Multiplication comes before addition.
Solution
Answer: 14
Multiplication first: 3 × 4 = 12, then 2 + 12 = 14.
4Explore
Divide and keep the remainder
Pick a and b: / keeps the decimals, % gives what is left over, and Math.floor(a / b) gives the whole number of times b fits.
const a = 7;
const b = 2;
console.log(a / b, a % b, Math.floor(a / b));3.5 1 35Problem
17 % 5
What does this print?
console.log(17 % 5);Hint
17 = 5 × 3 + ?
Solution
Answer: 2
17 = 5 × 3 + 2, so the remainder is 2.
6Explain
Shortcuts: += and ++
Updating a variable with its own value is so common that it has short forms. x += 5 means x = x + 5, and there are matching forms -=, *= and /=. Adding or taking away exactly one has an even shorter form: x++ adds 1 and x-- subtracts 1. You will see ++ constantly in loops.
Watch coins change on each line: += adds, *= multiplies, -- takes one away.
let coins = 10;coins += 5; // 15coins *= 2; // 30coins--; // 29console.log(coins); // 29
29
7Problem
+= then *=
What does this print?
let x = 10;
x += 4;
x *= 2;
console.log(x);Hint
10 + 4 first, then double it.
Solution
Answer: 28
10 + 4 = 14, then 14 × 2 = 28.
8Explain
The Math helpers
JavaScript comes with a built-in object called Math that holds ready-made number tools. Math.round(2.5) rounds to the nearest whole number (3), Math.floor(2.9) always rounds down (2), and Math.ceil(2.1) always rounds up (3). Math.max(4, 9, 2) gives the largest of its values and Math.min the smallest. Math.sqrt(81) is the square root, 9, and Math.PI is the number π, about 3.14159.
console.log(Math.round(2.5), Math.floor(2.9), Math.ceil(2.1));
console.log(Math.max(3, 11, 6), Math.min(3, 11, 6));
console.log(Math.PI.toFixed(2));3 2 3
11 3
3.149Explain
Why 0.1 + 0.2 is not 0.3
JavaScript stores every number in binary using a fixed amount of memory, following the IEEE 754 double-precision format that the ECMAScript standard specifies. Some ordinary decimals, such as 0.1, cannot be written exactly in binary, just as 1/3 cannot be written exactly in decimal. The tiny rounding errors usually stay hidden, but sometimes they show: 0.1 + 0.2 prints 0.30000000000000004.
console.log(0.1 + 0.2);
console.log((0.1 + 0.2).toFixed(2));0.30000000000000004
0.3010Practice
The average of three scores
Print the average of the three scores (it should be 85).
85let total = 80 + 90 + 85;
console.log(total);
Hint
The average is the total divided by how many scores there are.
A solution
let total = 80 + 90 + 85;
console.log(total / 3);11Practice
Minutes to hours
Use Math.floor and % so the program prints whole hours and leftover minutes: 2 15.
2 15const minutes = 135;
const hours = 0;
const rest = 0;
console.log(hours, rest);
Hint
Math.floor(minutes / 60) is the whole hours; minutes % 60 is what is left.
A solution
const minutes = 135;
const hours = Math.floor(minutes / 60);
const rest = minutes % 60;
console.log(hours, rest);12Explain
Common mistakes
const price = 4;
price += 1;
console.log(price);TypeError: Assignment to constant variable. (line 2)
- The word before the colon names the kind of error: ReferenceError, TypeError, SyntaxError.
- After the colon: what went wrong, usually with the name involved.
- The line number shows where the program stopped. A SyntaxError stops it before any line runs.
+= is an assignment too, so it cannot change a const. Declare price with let if it has to grow.
13
Summary
- + - * / work as in maths; / keeps decimals.
- % gives the remainder; n % 2 === 0 means n is even.
- ** is power. Parentheses go first, then **, then * / %, then + -.
- x += 5 and x++ are short forms for updating x.
- Math.floor, Math.round, Math.ceil, Math.max, Math.min, Math.PI.
- Decimals can carry tiny rounding errors; use toFixed to display them.