Math is a built-in object that has properties and methods for mathematical constants and functions. Not a function object.
Math works with the Number type. It doesn't work with BigInt.
Description
Unlike the other global objects, Math is not a constructor. All properties and methods of Math are static. You refer to the constant pi as Math.PI and you call the sine function as Math.sin(x), where x is the method's argument. Constants are defined with the full precision of real numbers in JavaScript.
Properties
Math.E- Euler's constant and the base of natural logarithms; approximately
2.718. Math.LN2- Natural logarithm of
2; approximately0.693. Math.LN10- Natural logarithm of
10; approximately2.303. Math.LOG2E- Base 2 logarithm of
E; approximately1.443. Math.LOG10E- Base 10 logarithm of
E; approximately0.434. Math.PI- Ratio of the a circle's circumference to its diameter; approximately
3.14159. Math.SQRT1_2- Square root of ½ (or equivalently, 1/√2 over the square root of 2); approximately
0.707. Math.SQRT2- Square root of
2; approximately1.414.
Methods
Note that trigonometric functions (sin(), cos(), tan(), asin(), acos(), atan(), atan2()) expect (and return) angles in radians.
To convert radians to degrees, divide by (Math.PI / 180). Multiply by the same value to convert degrees to radians.
Note: Many Math functions have a precision that's implementation-dependent. This means that different browsers can give a different result. Even the same JavaScript engine on a different OS or architecture can give different results!
Math.abs(x)- Returns the absolute value of a number.
Math.acos(x)- Returns the arccosine of a number.
Math.acosh(x)- Returns the hyperbolic arccosine of a number.
Math.asin(x)- Returns the arcsine of a number.
Math.asinh(x)- Returns the hyperbolic arcsine of a number.
Math.atan(x)- Returns the arctangent of a number.
Math.atanh(x)- Returns the hyperbolic arctangent of a number.
Math.atan2(y, x)- Returns the arctangent of the quotient of its arguments.
Math.cbrt(x)- Returns the cube root of a number.
Math.ceil(x)- Returns the smallest integer greater than or equal to a number.
Math.clz32(x)- Returns the number of leading zeroes of a 32-bit integer.
Math.cos(x)- Returns the cosine of a number.
Math.cosh(x)- Returns the hyperbolic cosine of a number.
Math.exp(x)- Returns
Ex, wherexis the argument, andEis Euler's constant (2.718…, the base of the natural logarithm). Math.expm1(x)- Returns subtracting
1fromexp(x). Math.floor(x)- Returns the largest integer less than or equal to a number.
Math.fround(x)- Returns the nearest single precision float representation of a number.
Math.hypot([x[, y[, …]]])- Returns the square root of the sum of squares of its arguments.
Math.imul(x, y)- Returns the result of a 32-bit integer multiplication.
Math.log(x)- Returns the natural logarithm (㏒e; also, ㏑) of a number.
Math.log1p(x)- Returns the natural logarithm (㏒e; also ㏑) of
1 + xfor a number. Math.log10(x)- Returns the base 10 logarithm of a number.
Math.log2(x)- Returns the base 2 logarithm of a number.
Math.max([x[, y[, …]]])- Returns the largest of zero or more numbers.
Math.min([x[, y[, …]]])- Returns the smallest of zero or more numbers.
Math.pow(x, y)- Returns base to the exponent power, that is,
xy. Math.random()- Returns a pseudo-random number between
0and1. Math.round(x)- Returns the value of a number rounded to the nearest integer.
Math.sign(x)- Returns the sign of the
x, indicating whetherxis positive, negative, or zero. Math.sin(x)- Returns the sine of a number.
Math.sinh(x)- Returns the hyperbolic sine of a number.
Math.sqrt(x)- Returns the positive square root of a number.
Math.tan(x)- Returns the tangent of a number.
Math.tanh(x)- Returns the hyperbolic tangent of a number.
Math.toSource()- Returns the string
"Math". Math.trunc(x)- Returns the integer part of the number
x, removing any fractional digits.
Extending the Math object
As with most of the built-in objects in JavaScript, the Math object can be extended with custom properties and methods. To extend the Math object, you cannot use prototype. Instead, you directly extend Math:
Math.propName = propValue Math.methodName = methodRef
For instance, the following example adds a method to the Math object for calculating the greatest common divisor of a list of arguments.
/* Variadic function -- Returns the greatest common divisor of a list of arguments */
Math.gcd = function() {
if (arguments.length == 2) {
if (arguments[1] == 0) {
return arguments[0]
}
else {
return Math.gcd(arguments[1], arguments[0] % arguments[1])
}
} else if (arguments.length > 2) {
let result = Math.gcd(arguments[0], arguments[1])
for (let i = 2; i < arguments.length; i++) {
result = Math.gcd(result, arguments[i])
}
return result
}
}
Try it:
console.log(Math.gcd(20, 30, 15, 70, 40)) // `5`
Specifications
| Specification | Status | Comment |
|---|---|---|
| ECMAScript 1st Edition (ECMA-262) | Standard | Initial definition. Implemented in JavaScript 1.1. |
| ECMAScript 5.1 (ECMA-262) The definition of 'Math' in that specification. |
Standard | |
| ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Math' in that specification. |
Standard | New methods log10(), log2(), log1p(), expm1(), cosh(), sinh(), tanh(), acosh(), asinh(), atanh(), hypot(), trunc(), sign(), imul(), fround(), cbrt() and clz32() added. |
| ECMAScript Latest Draft (ECMA-262) The definition of 'Math' in that specification. |
Draft |
Browser compatibility
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
E | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 3 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
LN2 | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 3 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
LN10 | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 3 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
LOG2E | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 3 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
LOG10E | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 3 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
PI | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 3 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
SQRT1_2 | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 3 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
SQRT2 | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 3 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
abs | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 3 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
acos | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 3 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
acosh | Chrome Full support 38 | Edge Full support 12 | Firefox Full support 25 | IE No support No | Opera Full support 25 | Safari Full support 8 | WebView Android Full support Yes | Chrome Android Full support 38 | Firefox Android Full support 25 | Opera Android Full support Yes | Safari iOS Full support 8 | Samsung Internet Android Full support Yes | nodejs Full support 0.12 |
asin | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 3 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
asinh | Chrome Full support 38 | Edge Full support 12 | Firefox Full support 25 | IE No support No | Opera Full support 25 | Safari Full support 8 | WebView Android Full support Yes | Chrome Android Full support 38 | Firefox Android Full support 25 | Opera Android Full support Yes | Safari iOS Full support 8 | Samsung Internet Android Full support Yes | nodejs Full support 0.12 |
atan | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 3 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
atan2 | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 4 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
atanh | Chrome Full support 38 | Edge Full support 12 | Firefox Full support 25 | IE No support No | Opera Full support 25 | Safari Full support 8 | WebView Android Full support Yes | Chrome Android Full support 38 | Firefox Android Full support 25 | Opera Android Full support Yes | Safari iOS Full support 8 | Samsung Internet Android Full support Yes | nodejs Full support 0.12 |
cbrt | Chrome Full support 38 | Edge Full support 12 | Firefox Full support 25 | IE No support No | Opera Full support 25 | Safari Full support 8 | WebView Android Full support Yes | Chrome Android Full support 38 | Firefox Android Full support 25 | Opera Android Full support Yes | Safari iOS Full support 8 | Samsung Internet Android Full support Yes | nodejs Full support 0.12 |
ceil | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 3 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
clz32 | Chrome Full support 38 | Edge Full support 12 | Firefox Full support 31 | IE No support No | Opera Full support 25 | Safari Full support Yes | WebView Android Full support Yes | Chrome Android Full support 38 | Firefox Android Full support 31 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support Yes | nodejs Full support 0.12 |
cos | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 3 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
cosh | Chrome Full support 38 | Edge Full support 12 | Firefox Full support 25 | IE No support No | Opera Full support 25 | Safari Full support 8 | WebView Android Full support Yes | Chrome Android Full support 38 | Firefox Android Full support 25 | Opera Android Full support Yes | Safari iOS Full support 8 | Samsung Internet Android Full support Yes | nodejs Full support 0.12 |
exp | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 3 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
expm1 | Chrome Full support 38 | Edge Full support 12 | Firefox Full support 25 | IE No support No | Opera Full support 25 | Safari Full support 8 | WebView Android Full support Yes | Chrome Android Full support 38 | Firefox Android Full support 25 | Opera Android Full support Yes | Safari iOS Full support 8 | Samsung Internet Android Full support Yes | nodejs Full support 0.12 |
floor | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 3 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
fround | Chrome Full support 38 | Edge Full support 12 | Firefox Full support 26 | IE No support No | Opera Full support 25 | Safari Full support 8 | WebView Android Full support Yes | Chrome Android Full support 38 | Firefox Android Full support 26 | Opera Android Full support Yes | Safari iOS Full support 8 | Samsung Internet Android Full support Yes | nodejs Full support 0.12 |
hypot | Chrome Full support 38 | Edge Full support 12 | Firefox Full support 27 | IE No support No | Opera Full support 25 | Safari Full support 8 | WebView Android Full support Yes | Chrome Android Full support 38 | Firefox Android Full support 27 | Opera Android Full support Yes | Safari iOS Full support 8 | Samsung Internet Android Full support Yes | nodejs Full support 0.12 |
imul | Chrome Full support 28 | Edge Full support 12 | Firefox Full support 20 | IE No support No | Opera Full support 16 | Safari Full support 7 | WebView Android Full support Yes | Chrome Android Full support 28 | Firefox Android Full support 20 | Opera Android Full support Yes | Safari iOS Full support 7 | Samsung Internet Android Full support Yes | nodejs Full support 0.12 |
log | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 3 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
log1p | Chrome Full support 38 | Edge Full support 12 | Firefox Full support 25 | IE No support No | Opera Full support 25 | Safari Full support 8 | WebView Android Full support Yes | Chrome Android Full support 38 | Firefox Android Full support 25 | Opera Android Full support Yes | Safari iOS Full support 8 | Samsung Internet Android Full support Yes | nodejs Full support 0.12 |
log2 | Chrome Full support 38 | Edge Full support 12 | Firefox Full support 25 | IE No support No | Opera Full support 25 | Safari Full support 8 | WebView Android Full support Yes | Chrome Android Full support 38 | Firefox Android Full support 25 | Opera Android Full support Yes | Safari iOS Full support 8 | Samsung Internet Android Full support Yes | nodejs Full support 0.12 |
log10 | Chrome Full support 38 | Edge Full support 12 | Firefox Full support 25 | IE No support No | Opera Full support 25 | Safari Full support 8 | WebView Android Full support Yes | Chrome Android Full support 38 | Firefox Android Full support 25 | Opera Android Full support Yes | Safari iOS Full support 8 | Samsung Internet Android Full support Yes | nodejs Full support 0.12 |
max | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 3 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
min | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 3 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
pow | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 3 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
random | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 3 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
round | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 3 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
sign | Chrome Full support 38 | Edge Full support 12 | Firefox Full support 25 | IE No support No | Opera Full support 25 | Safari Full support 9 | WebView Android Full support Yes | Chrome Android Full support 38 | Firefox Android Full support 25 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support Yes | nodejs Full support 0.12 |
sin | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 3 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
sinh | Chrome Full support 38 | Edge Full support 12 | Firefox Full support 25 | IE No support No | Opera Full support 25 | Safari Full support 8 | WebView Android Full support Yes | Chrome Android Full support 38 | Firefox Android Full support 25 | Opera Android Full support Yes | Safari iOS Full support 8 | Samsung Internet Android Full support Yes | nodejs Full support 0.12 |
sqrt | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 3 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
tan | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 3 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
tanh | Chrome Full support 38 | Edge Full support 12 | Firefox Full support 25 | IE No support No | Opera Full support 25 | Safari Full support 8 | WebView Android Full support Yes | Chrome Android Full support 38 | Firefox Android Full support 25 | Opera Android Full support Yes | Safari iOS Full support 8 | Samsung Internet Android Full support Yes | nodejs Full support 0.12 |
trunc | Chrome Full support 38 | Edge Full support 12 | Firefox Full support 25 | IE No support No | Opera Full support 25 | Safari Full support 8 | WebView Android Full support Yes | Chrome Android Full support 38 | Firefox Android Full support 25 | Opera Android Full support Yes | Safari iOS Full support 8 | Samsung Internet Android Full support Yes | nodejs Full support 0.12 |
Legend
- Full support
- Full support
- No support
- No support