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.floorrounds down to a whole number%gives the remainder, so17 % 5is 2.toFixed(2)turns a number into text with two decimal places
Common mistakes
"5" + 3is"53":+joins text whenever either side is a string.0.1 + 0.2 === 0.3is false. Round before comparing decimals.

Cloud tip:
n % 2 === 0 checks whether a number is even.

