The Math Object

The built-in Math object holds mathematical constants and functions: rounding, powers and roots, minimum and maximum, trigonometry, and random numbers. You never create it — you just call its methods directly, like Math.round(2.5).

Rounding

JS rounding.js
const n = 4.567;

console.log(Math.round(n));
console.log(Math.floor(n));
console.log(Math.ceil(n));
console.log(Math.trunc(n));
console.log(Math.round(-4.5), Math.floor(-4.5), Math.trunc(-4.5));
Output
5
4
5
4
-4 -5 -4
MethodDoes
Math.roundNearest integer (halves round up, towards +∞)
Math.floorDown, towards −∞
Math.ceilUp, towards +∞
Math.truncRemoves the decimal part (towards 0)

The difference between floor and trunc only shows up with negative numbers.

Rounding to decimal places

JS decimals.js
function roundTo(value, places) {
  const factor = 10 ** places;
  return Math.round(value * factor) / factor;
}

console.log(roundTo(3.14159, 2));
console.log(roundTo(2.675, 1));
Output
3.14
2.7

This returns a number, whereas toFixed returns a string. Choose based on whether you'll keep calculating or just display the result.

Powers, roots and absolute value

JS powers.js
console.log(Math.pow(2, 8), 2 ** 8);
console.log(Math.sqrt(144));
console.log(Math.cbrt(27));
console.log(Math.abs(-12.5));
console.log(Math.sign(-3), Math.sign(0), Math.sign(9));
console.log(Math.hypot(3, 4));
Output
256 256
12
3
12.5
-1 0 1
5

Math.hypot(3, 4) calculates the length of the hypotenuse — handy for the distance between two points.

Minimum and maximum

JS min-max.js
console.log(Math.max(3, 9, 2));
console.log(Math.min(3, 9, 2));

const scores = [71, 94, 88];
console.log(Math.max(...scores));

function clamp(value, min, max) {
  return Math.min(Math.max(value, min), max);
}
console.log(clamp(150, 0, 100), clamp(-5, 0, 100), clamp(42, 0, 100));
Output
9
2
94
100 0 42

Math.max takes separate arguments, not an array, so spread the array with .... The clamp helper keeps a value inside a range — useful for volume sliders, scroll positions and progress bars.

Random numbers

Math.random() returns a decimal from 0 (inclusive) up to, but not including, 1. Scale and round it to get what you need:

JS random.js
function randomInt(min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

const dice = randomInt(1, 6);
console.log(dice >= 1 && dice <= 6);

const colors = ["red", "green", "blue"];
const pick = colors[Math.floor(Math.random() * colors.length)];
console.log(colors.includes(pick));
Output
true
true

The output of Math.random is different every time, so this example only checks the result is in range. randomInt includes both min and max.

Security note: Math.random is not suitable for passwords, tokens or anything security-related, because its output can be predicted. Use crypto.getRandomValues() or crypto.randomUUID() instead.

Constants and trigonometry

JS trig.js
console.log(Math.PI.toFixed(5));
console.log(Math.E.toFixed(5));

const degrees = 90;
const radians = degrees * (Math.PI / 180);
console.log(Math.sin(radians));
console.log(Math.round(Math.cos(radians) * 1000) / 1000);
console.log(Math.log10(1000), Math.log2(8));
Output
3.14159
2.71828
1
0
3 3

Trigonometric functions work in radians, so convert from degrees first. You'll use them for animations, games and drawing on a <canvas>.

Try it yourself

Try it yourself
Console output