Skip to content
dreamcode
dreamcode
Map
Numbers & Math
Lesson 2 of 48
+15 XP on finish
JS BASICSChapter 1 · JS Basics

Numbers and Math

JavaScript has one number type for whole numbers and decimals alike. Use +, -, * and / for arithmetic, % for the remainder and ** for powers. The built-in Math object has the helpers: Math.round, Math.floor, Math.max, Math.random and more. One surprise: 0.1 + 0.2 is not exactly 0.3, because decimals are stored in binary.

Worked example

How it reads

  • / keeps the decimals; Math.floor rounds down to a whole number
  • % gives the remainder, so 17 % 5 is 2
  • .toFixed(2) turns a number into text with two decimal places
Common mistakes
  • "5" + 3 is "53": + joins text whenever either side is a string.
  • 0.1 + 0.2 === 0.3 is false. Round before comparing decimals.
Cloud tip: n % 2 === 0 checks whether a number is even.
index.js
JAVASCRIPT
real JavaScript, runs in your browser
Console
Run your code to see its output here.
YOUR TURN

Also print how many minutes are left after the whole hours. The program should print 2 and then 15.

Press Run to check your work.