0
JavaScript · JavaScript Basics
New technique
Doing the maths
Every forge needs a ledger. Let's count iron bars the JavaScript way.
JavaScript does the maths you know from school, with the usual order: * and / before + and -, and parentheses first. All numbers share one type, number, whether they're whole or have decimals.
Some sums have no sensible answer. Instead of crashing, JavaScript gives a special value and carries on, so a typo can quietly spread through your sums.
7 + 2 // 9 7 - 2 // 5 7 * 2 // 14 7 / 2 // 3.5 7 % 2 // 1 - remainder after dividing 2 ** 3 // 8 - 2 to the power 3 5 / 0 // Infinity 0 / 0 // NaN - "Not a Number"
%gives the remainder: handy for "is this number even?" (n % 2 === 0).**raises to a power.NaNis what you get from maths that makes no sense. Its type is still"number".