The Math Object

JavaScript ES2024 Β· βœ“ verified by execution on 2026-08-15

JavaScript provides a built-in object called Math that acts as a toolbox for performing mathematical tasks.

Unlike objects such as Date where you must use the new keyword to create an instance (new Date()), the Math object is static. You simply call its methods directly whenever you need them.

Generating Random Numbers

The most commonly used method is Math.random(). It generates a pseudo-random decimal number ranging from 0 (inclusive) up to, but strictly less than, 1 (exclusive).

javascript
const r = Math.random();
// It will always be a number >= 0 and < 1
console.log(typeof r === 'number' && r >= 0 && r < 1);
Output
true

Because it only returns a decimal between 0 and 0.9999..., you usually need to scale it to fit your needs. For example, if you want a random number between 0 and 10, you multiply the result by 10:

javascript Β· visualize
const scaled = Math.random() * 10;

Math.random() [ 0.0 to 0.999... ] Multiply by 10 Scaled Result [ 0.0 to 9.999... ]
Visualizing how Math.random() is scaled.

However, scaling alone leaves you with a messy decimal (e.g., 7.349182). To get clean integers, you need rounding methods.

Rounding Methods

JavaScript provides three primary methods for removing decimals:

  1. Math.round(x): Rounds to the nearest integer.
  2. Math.floor(x): Always rounds down to the nearest integer.
  3. Math.ceil(x): Always rounds up to the nearest integer.
javascript
console.log(Math.round(4.5)); // Nearest integer
console.log(Math.floor(4.9)); // Force down
console.log(Math.ceil(4.1));  // Force up
Output
5
4
5

[!WARNING] There is a notoriously tricky edge case with negative numbers. In JavaScript, Math.round() always rounds β€œ.5” UP towards positive infinity. Therefore, Math.round(-4.5) evaluates to -4, not -5!

Predict the output javascript

Read the code. What exactly will it print? Commit to an answer before you look.

// What do you think this evaluates to?
console.log(Math.round(-4.5));
Output
-4

Creating a Random Integer

By combining Math.random() and Math.floor(), you can easily generate whole numbers. To get a random integer from 0 to 9:

javascript
const randomDigit = Math.floor(Math.random() * 10);
console.log(typeof randomDigit === 'number');
Output
true

Finding Min and Max

You can find the lowest or highest value in a sequence using Math.min() and Math.max().

javascript
console.log(Math.max(10, 50, 20));
Output
50

Notice that we passed the numbers as separated, positional arguments. A common beginner mistake is passing an array directly into Math.max().

If you do this, JavaScript won’t know how to evaluate the array as a single number and will return NaN (Not a Number).

javascript
console.log(Math.max([10, 50, 20]));
Output
NaN

To fix this, you must use the spread syntax (...) to unpack the array into individual arguments before passing them into the method: Math.max(...[10, 50, 20]).

Check yourself

Which expression correctly generates a random decimal number between 0 (inclusive) and 1 (exclusive)?

Reveal answer

Math.random() β€” Math is a static object, so you do not use "new". You call the random() method directly on it.

What does Math.round(-4.5) evaluate to?

Reveal answer

-4 β€” In JavaScript, Math.round() always rounds half UP towards positive infinity. Therefore, -4.5 rounds up to -4.

What is the result of Math.floor(4.9)?

Reveal answer

4 β€” Math.floor() ALWAYS rounds down to the nearest integer, chopping off the decimal entirely.

Why does Math.max([10, 50, 20]) return NaN?

Reveal answer

Because Math.max takes positional arguments, not an array. β€” Math.max() expects numbers separated by commas (e.g. Math.max(10, 50, 20)). If you pass a single array, it cannot interpret it as a number and returns NaN. You must use the spread operator (...array).

Challenges

Challenge 1 +15 XP

Write a program that uses Math.random() to simulate a 6-sided die roll (returning a whole number from 1 to 6).

javascript
  • Test 1 (input: "0.0") β€” expects "1"
  • Test 2 (input: "0.9999") β€” expects "6"
  • Test 3 (input: "0.5") β€” expects "4"
Need a hint? (βˆ’25% XP)

Multiply Math.random() by 6, then use Math.floor() to remove decimals. Finally, add 1 so the result is 1-6 instead of 0-5.

Show solution (0 XP)
import fs from 'node:fs';
const seed = parseFloat(fs.readFileSync(0, 'utf-8'));

Math.random = () => seed;

function rollDie() {
  return Math.floor(Math.random() * 6) + 1;
}

console.log(rollDie());

🐞 Bug Hunt +10 XP

Find the highest score in the array. The current code returns NaN because Math.max doesn't accept arrays.

This code runs. It just does the wrong thing. Read it, find the defect, fix it β€” the tests below decide when you are right.

javascript
  • Test 1 β€” expects "100"
Need a hint? (βˆ’25% XP)

Use the spread syntax (...) to expand the array elements into separate arguments.

Show solution (0 XP)
const scores = [85, 92, 100, 78];

const highestScore = Math.max(...scores);
console.log(highestScore);