edubun
English
1 / 13

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 questions

You 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.

OperatorMeaningExampleResult
+add7 + 29
-subtract7 - 25
*multiply7 * 214
/divide7 / 23.5
%remainder7 % 21
**power7 ** 249

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);     // 40
Output
14
20
17
40

3Problem

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.

a
b
const a = 7;
const b = 2;
console.log(a / b, a % b, Math.floor(a / b));
Output
3.5 1 3

5Problem

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.

  1. let coins = 10;
  2. coins += 5; // 15
  3. coins *= 2; // 30
  4. coins--; // 29
  5. console.log(coins); // 29
Variables
Output
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));
Output
3 2 3
11 3
3.14

9Explain

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));
Output
0.30000000000000004
0.30

10Practice

The average of three scores

Print the average of the three scores (it should be 85).

Expected output
85
Your code
let 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.

Expected output
2 15
Your code
const 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);
Error message
TypeError: Assignment to constant variable. (line 2)
  1. The word before the colon names the kind of error: ReferenceError, TypeError, SyntaxError.
  2. After the colon: what went wrong, usually with the name involved.
  3. 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.

Next unitStrings and template literals
Full text · The whole lesson as one text

The arithmetic operators

JavaScript can calculate 7 + 2 just as you would on paper. Most of what computers do comes down to arithmetic, and JavaScript writes it much as you would on paper. The symbols that do the work are called operators. Five of them you already know, and two are new:

OperatorMeaningExampleResult
+add7 + 29
-subtract7 - 25
*multiply7 * 214
/divide7 / 23.5
%remainder7 % 21
**power7 ** 249

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. That sounds like a curiosity, but it is one of the most used operators in programming. A number is even exactly when n % 2 is 0, and minutes % 60 gives the minutes left after taking out whole hours.

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);     // 40

Shortcuts for updating a variable

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.

let coins = 10;
coins += 5;      // 15
coins *= 2;      // 30
coins--;         // 29
console.log(coins);   // 29

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. Math.floor and % make a good pair: for 135 minutes, Math.floor(135 / 60) is 2 hours and 135 % 60 is 15 minutes.

Why 0.1 + 0.2 is not exactly 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. This is not a bug in your code. When you show money or measurements to people, round the result, for example with (0.1 + 0.2).toFixed(2), which gives the string "0.30". Whole numbers from −9007199254740991 to 9007199254740991 (about nine thousand million million) are safe: they are stored exactly. Beyond that range, adding 1 may not change the stored value.

Worked examples

Example 1. The average of 80, 90 and 85 is (80 + 90 + 85) / 3. Without parentheses, 80 + 90 + 85 / 3 divides only the 85 and gives about 198.3. Example 2. Is 2024 a multiple of 4? 2024 % 4 is 0, so yes. Example 3. Splitting 50 sweets among 6 children: Math.floor(50 / 6) is 8 each and 50 % 6 is 2 left over.

Key points
  • + - * / 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.

Practice questions from Numbers and math on edubun

Embed this quiz

Free to embed. Please keep the credit line.

Sources for this course (2)
  • MDN Web Docs, JavaScript Guide (developer.mozilla.org/en-US/docs/Web/JavaScript/Guide, chapters Introduction, Grammar and types, Control flow, Loops and iteration, Functions, Expressions and operators, Numbers and strings, Indexed collections, Working with objects; checked 2026-10-17)
  • Ecma International, ECMA-262 ECMAScript 2026 Language Specification, 17th edition, June 2026 (tc39.es/ecma262 and ecma-international.org/publications-and-standards/standards/ecma-262, checked 2026-10-17).

Checked: 2026-10-17