Numbers and Math
Numbers power the calculations behind every web application: prices, quantities, discounts, ratings. JavaScript handles numbers differently from most languages, and knowing the quirks early will save you from confusing bugs.
One number type
Section titled “One number type”JavaScript has a single number type for both integers and decimals:
const tourPrice = 149; // integerconst taxRate = 0.08; // decimal (float)const discount = 14.9; // decimalThere is no separate int or float type. They are all just number.
Arithmetic operators
Section titled “Arithmetic operators”The basic math operators work as expected:
| Operator | Name | Example | Result |
|---|---|---|---|
+ | Addition | 149 + 20 | 169 |
- | Subtraction | 149 - 20 | 129 |
* | Multiplication | 149 * 2 | 298 |
/ | Division | 149 / 2 | 74.5 |
% | Remainder | 149 % 10 | 9 |
** | Exponentiation | 2 ** 8 | 256 |
The remainder operator (%) returns what is left over after whole division. 149 % 10 is 9 because 149 divided by 10 is 14 with 9 remaining.
Standard order of operations applies: multiplication and division before addition and subtraction. Use parentheses to be explicit:
const discountedPrice = tourPrice * (1 - 0.10); // 10% off: 134.1Floating point quirks
Section titled “Floating point quirks”This surprises almost every developer the first time:
console.log(0.1 + 0.2); // 0.30000000000000004This is not a JavaScript bug — it is how binary floating-point arithmetic works in virtually every programming language. The workaround is to round when displaying values:
const result = 0.1 + 0.2;console.log(result.toFixed(2)); // '0.30'For financial calculations, multiply to integers, do the math, then divide back:
const priceInCents = Math.round(tourPrice * 100); // work in centsNaN stands for “Not a Number.” You get it when a math operation produces an invalid result:
console.log('Hike' * 2); // NaN — cannot multiply a stringconsole.log(0 / 0); // NaNconsole.log(parseInt('abc')); // NaNNaN is contagious — any arithmetic involving NaN produces NaN. Check for it with Number.isNaN():
const result = parseInt('abc');console.log(Number.isNaN(result)); // trueThe Math object
Section titled “The Math object”JavaScript has a built-in Math object with useful numeric operations:
Math.round(149.6); // 150 — rounds to nearest integerMath.floor(149.9); // 149 — always rounds downMath.ceil(149.1); // 150 — always rounds upMath.max(149, 199, 249); // 249 — largest valueMath.min(149, 199, 249); // 149 — smallest valueMath.random(); // 0.something — random decimal between 0 and 1Math.random() returns a decimal between 0 (inclusive) and 1 (exclusive). To get a random integer in a range:
const randomIndex = Math.floor(Math.random() * 4); // 0, 1, 2, or 3.toFixed()
Section titled “.toFixed()”.toFixed(n) formats a number as a string with exactly n decimal places:
const price = 149;console.log(price.toFixed(2)); // '149.00'
const discount = 14.9;console.log(discount.toFixed(2)); // '14.90'Note that .toFixed() returns a string, not a number. Use it when displaying prices to users, not for further calculations.
Exercise
Section titled “Exercise”Calculate a 10% discount on the STO tour price and display it formatted:
const tourPrice = 149;
const discountAmount = tourPrice * 0.10;const discountedPrice = tourPrice - discountAmount;
console.log('Original:', tourPrice.toFixed(2));console.log('Discount:', discountAmount.toFixed(2));console.log('Final price:', discountedPrice.toFixed(2));
const rounded = Math.round(discountedPrice);console.log('Rounded final:', rounded);Also log 0.1 + 0.2 and then (0.1 + 0.2).toFixed(2) to see the floating point quirk and its fix side by side.
- JavaScript uses a single number type for both integers and decimals.
- Basic arithmetic:
+,-,*,/,%(remainder),**(exponentiation). - Floating point arithmetic is imprecise —
0.1 + 0.2 !== 0.3. Use.toFixed()when displaying results. NaN(“Not a Number”) appears when a math operation is invalid. Check withNumber.isNaN().Math.round(),Math.floor(),Math.ceil(),Math.max(),Math.min(), andMath.random()cover the most common numeric operations..toFixed(n)returns a string formatted tondecimal places — use it for display, not further calculations.