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 < 1console.log(typeof r === 'number' && r >= 0 && r < 1);
Output
true
Your output
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;
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:
Math.round(x): Rounds to the nearest integer.
Math.floor(x): Always rounds down to the nearest integer.
Math.ceil(x): Always rounds up to the nearest integer.
javascript
console.log(Math.round(4.5)); // Nearest integerconsole.log(Math.floor(4.9)); // Force downconsole.log(Math.ceil(4.1)); // Force up
Output
5
4
5
Your output
[!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 outputjavascript
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
You predicted
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:
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
Your output
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
Your output
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.