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
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));
5 4 5 4 -4 -5 -4
| Method | Does |
|---|---|
Math.round | Nearest integer (halves round up, towards +∞) |
Math.floor | Down, towards −∞ |
Math.ceil | Up, towards +∞ |
Math.trunc | Removes the decimal part (towards 0) |
The difference between floor and trunc only shows up with negative numbers.
Rounding to decimal places
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));
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
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));
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
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));
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:
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));
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.
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
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));
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>.