JavaScript (ES2026) reference · 23 entries
JavaScript Math Methods and Constants
Every commonly used Math function and constant, from abs() and round() to random() and sumPrecise(), with examples for rounding, ranges, geometry and logarithms.
Math is a built-in object, not a constructor: call its functions directly, as in Math.round(2.5). All of
them work with JavaScript's 64-bit floating-point numbers; use BigInt for exact large integers.
| Name | Description | Since |
|---|---|---|
Math.abs() |
Return the absolute value of a number: its distance from zero, so Math.abs(-5) is 5. | ES1 |
Math.ceil() |
Round a number up to the next integer: Math.ceil(4.1) is 5 and Math.ceil(-4.1) is -4. | ES1 |
Math.floor() |
Round a number down to the integer below it: Math.floor(4.9) is 4 and Math.floor(-4.1) is -5. | ES1 |
Math.round() |
Round a number to the nearest integer; halves round up towards +Infinity, so Math.round(-2.5) is -2. | ES1 |
Math.trunc() |
Remove the fractional part of a number, rounding towards zero: Math.trunc(-4.7) is -4. | ES2015 |
Math.sign() |
Return 1, -1 or 0 depending on whether a number is positive, negative or zero. | ES2015 |
Math.max() |
Return the largest of the given numbers. Use the spread syntax to find the largest value in an array. | ES1 |
Math.min() |
Return the smallest of the given numbers. Use spread to find the smallest value in an array. | ES1 |
Math.pow() |
Return a base raised to an exponent. The ** operator does the same: Math.pow(2, 10) equals 2 ** 10. | ES1 |
Math.sqrt() |
Return the square root of a number, or NaN for negative numbers. Use it for distances and the Pythagorean theorem. | ES1 |
Math.cbrt() |
Return the cube root of a number; unlike Math.pow(x, 1/3) it works for negative numbers. | ES2015 |
Math.hypot() |
Return the square root of the sum of squares of its arguments, e.g. the length of a vector or a diagonal. | ES2015 |
Math.random() |
Return a pseudo-random number from 0 (inclusive) up to 1 (exclusive). Not suitable for security; use crypto for that. | ES1 |
Math.log() |
Return the natural logarithm (base e) of a number; divide by Math.log(base) to get a logarithm in any base. | ES1 |
Math.log10() |
Return the base-10 logarithm of a number, e.g. to count digits or work with decibels. | ES2015 |
Math.log2() |
Return the base-2 logarithm of a number, e.g. how many bits or how many halvings are needed. | ES2015 |
Math.exp() |
Return e (about 2.718) raised to the given power, used for exponential growth, decay and probability formulas. | ES1 |
Math.sin() |
Return the sine of an angle given in radians. Convert degrees with deg * Math.PI / 180 first. | ES1 |
Math.cos() |
Return the cosine of an angle given in radians. Convert degrees with deg * Math.PI / 180 first. | ES1 |
Math.atan2() |
Return the angle in radians between the positive x axis and the point (x, y). Note the argument order: y first. | ES1 |
Math.PI |
The ratio of a circle's circumference to its diameter, approximately 3.14159. | ES1 |
Math.E |
Euler's number, the base of natural logarithms, approximately 2.71828. | ES1 |
Math.sumPrecise() |
Add up an iterable of numbers with full precision, avoiding the rounding errors of repeated + additions. | ES2026 |