The JavaScript Math object provides built-in constants and methods for common mathematical operations. You can use it for rounding numbers, generating random values, finding minimum and maximum values, calculating powers, and working with trigonometry.
Table of Contents
Math is a built-in object, so you do not need to import anything before using it.
Number constants
Constants are fixed values available on the Math object.
| Constant | Meaning | Example value |
|---|---|---|
Math.PI | Ratio of a circle’s circumference to its diameter | 3.141592653589793 |
Math.E | Euler’s number | 2.718281828459045 |
Math.LN2 | Natural logarithm of 2 | 0.6931471805599453 |
Math.LN10 | Natural logarithm of 10 | 2.302585092994046 |
Math.SQRT2 | Square root of 2 | 1.4142135623730951 |
Example:
const radius = 5;
const area = Math.PI * radius * radius;
console.log(area);Rounding methods
Rounding methods are useful when you need whole numbers from decimal values.
| Method | What it does | Example |
|---|---|---|
Math.ceil() | Rounds up to the next integer | Math.ceil(4.1) returns 5 |
Math.floor() | Rounds down to the previous integer | Math.floor(4.9) returns 4 |
Math.round() | Rounds to the nearest integer | Math.round(4.5) returns 5 |
Math.trunc() | Removes the decimal part | Math.trunc(4.9) returns 4 |
Example:
console.log(Math.ceil(2.1)); // 3
console.log(Math.floor(2.9)); // 2
console.log(Math.round(2.5)); // 3
console.log(Math.trunc(2.9)); // 2Use Math.floor() when generating random indexes because array indexes start at 0.
Arithmetic methods
These methods help with common calculations.
| Method | Description | Example |
|---|---|---|
Math.pow(base, exponent) | Raises a number to a power | Math.pow(2, 3) returns 8 |
Math.sqrt(number) | Finds the square root | Math.sqrt(16) returns 4 |
Math.cbrt(number) | Finds the cube root | Math.cbrt(8) returns 2 |
Math.abs(number) | Returns the positive value | Math.abs(-10) returns 10 |
Math.min(...numbers) | Returns the smallest value | Math.min(9, 8, 7) returns 7 |
Math.max(...numbers) | Returns the largest value | Math.max(9, 8, 7) returns 9 |
Example:
const prices = [299, 499, 199, 999];
console.log(Math.min(...prices)); // 199
console.log(Math.max(...prices)); // 999The spread operator ... passes each array item as an individual argument to Math.min() or Math.max().
Logarithmic and trigonometric methods
JavaScript also includes methods for advanced math.
Common logarithmic methods:
Math.log()returns the natural logarithm.Math.log2()returns the base-2 logarithm.Math.log10()returns the base-10 logarithm.
Common trigonometric methods:
Math.sin()Math.cos()Math.tan()
These are mostly used in charts, animations, canvas work, game logic, geometry, and scientific calculations.
Random numbers
Math.random() returns a decimal number greater than or equal to 0 and less than 1.
const decimal = Math.random();
console.log(decimal);Because the value is always below 1, you usually multiply it when you need a larger range.
const randomValue = Math.random() * 10;
console.log(randomValue);Generate a random integer
To generate a random integer, combine Math.random() with Math.floor().
const randomNumber = Math.floor(Math.random() * 10);
console.log(randomNumber);This returns a number from 0 to 9.
To generate a number from 1 to 10, add 1 after rounding down:
const randomNumber = Math.floor(Math.random() * 10) + 1;
console.log(randomNumber);Generate a random number between two values
Use this helper when you need a random integer between a minimum and maximum value, including both values:
function getRandomInteger(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
console.log(getRandomInteger(5, 15));How it works:
max - min + 1calculates the size of the range.Math.random()chooses a decimal inside that range.Math.floor()converts it to an integer.+ minshifts the result so it starts from the minimum value.
Common mistakes
- Using
Math.ceil(Math.random() * 10)when you need values from0to9.Math.ceil()can return10and never returns0. - Forgetting that
Math.random()never returns1. - Passing an array directly to
Math.min()orMath.max(). Use the spread operator:Math.max(...numbers). - Expecting
Math.round()to always round up. It only rounds up when the decimal part is.5or greater.
Quick reference
Math.PI;
Math.ceil(4.2);
Math.floor(4.8);
Math.round(4.5);
Math.trunc(4.8);
Math.pow(2, 3);
Math.sqrt(16);
Math.abs(-10);
Math.min(4, 8, 2);
Math.max(4, 8, 2);
Math.random();