Skip to content

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.

JavaScript has a single number type for both integers and decimals:

const tourPrice = 149; // integer
const taxRate = 0.08; // decimal (float)
const discount = 14.9; // decimal

There is no separate int or float type. They are all just number.

The basic math operators work as expected:

OperatorNameExampleResult
+Addition149 + 20169
-Subtraction149 - 20129
*Multiplication149 * 2298
/Division149 / 274.5
%Remainder149 % 109
**Exponentiation2 ** 8256

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.1

This surprises almost every developer the first time:

console.log(0.1 + 0.2); // 0.30000000000000004

This 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 cents

NaN stands for “Not a Number.” You get it when a math operation produces an invalid result:

console.log('Hike' * 2); // NaN — cannot multiply a string
console.log(0 / 0); // NaN
console.log(parseInt('abc')); // NaN

NaN is contagious — any arithmetic involving NaN produces NaN. Check for it with Number.isNaN():

const result = parseInt('abc');
console.log(Number.isNaN(result)); // true

JavaScript has a built-in Math object with useful numeric operations:

Math.round(149.6); // 150 — rounds to nearest integer
Math.floor(149.9); // 149 — always rounds down
Math.ceil(149.1); // 150 — always rounds up
Math.max(149, 199, 249); // 249 — largest value
Math.min(149, 199, 249); // 149 — smallest value
Math.random(); // 0.something — random decimal between 0 and 1

Math.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(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.

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 with Number.isNaN().
  • Math.round(), Math.floor(), Math.ceil(), Math.max(), Math.min(), and Math.random() cover the most common numeric operations.
  • .toFixed(n) returns a string formatted to n decimal places — use it for display, not further calculations.