edubun 한국어

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.

The arithmetic operators

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 always gives the exact answer, decimals included: 7 / 2 is 3.5, not 3. 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 up to about nine thousand million million are stored exactly, so counting is never affected.

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.

operatorA symbol that does work on values: + - * / % **
%Remainder after division: 17 % 5 is 2
**Power: 2 ** 3 is 8
+=x += 5 means x = x + 5
++Add exactly 1 to a variable
Math.floorRound down to a whole number
Math.roundRound to the nearest whole number
toFixed(2)Format a number with 2 decimal places, as a string
  1. What does this print?
    console.log(2 + 3 * 4);
  2. What does this print?
    console.log((2 + 3) * 4);
  3. What does this print?
    console.log(17 % 5);
  4. What does this print?
    console.log(9 / 2);
  5. What does this print?
    console.log(2 ** 5);
  6. What does this print?
    let x = 10;
    x += 4;
    x *= 2;
    console.log(x);
  7. What does this print?
    let n = 5;
    n++;
    n++;
    n--;
    console.log(n);
  8. What does this print?
    console.log(Math.floor(7.9), Math.ceil(7.1));
    • 8 8
    • 7 8
    • 7 7
  9. What does this print?
    console.log(Math.max(3, 11, 6) - Math.min(3, 11, 6));
  10. What does this print?
    console.log(10 - 4 - 3);
  11. Which expression is true for every even number n?
    • n % 2 === 0
    • n / 2 === 0
    • n % 2 === 1
  12. What does this print?
    console.log((0.1 + 0.2).toFixed(2));
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.