Math
is a built-in object that has properties and methods for mathematical constants and functions. Not a function object.
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.
Math.E
Math.LN2
Math.LN10
Math.LOG2E
Math.LOG10E
Math.PI
Math.SQRT1_2
Math.SQRT2
Note that the trigonometric functions (sin()
, cos()
, tan()
, asin()
, acos()
, atan()
, atan2()
) expect or return angles in radians. To convert radians to degrees, divide by (Math.PI / 180)
, and multiply by this to convert the other way.
Note that many math functions have a precision that's implementation-dependent. This means that different browsers can give a different result, and even the same JS engine on a different OS or architecture can give different results.
Math.abs(x)
Math.acos(x)
Math.acosh(x)
Math.asin(x)
Math.asinh(x)
Math.atan(x)
Math.atanh(x)
Math.atan2(y, x)
Math.cbrt(x)
Math.ceil(x)
Math.clz32(x)
Math.cos(x)
Math.cosh(x)
Math.exp(x)
Math.expm1(x)
exp(x)
.Math.floor(x)
Math.fround(x)
Math.hypot([x[, y[, …]]])
Math.imul(x, y)
Math.log(x)
Math.log1p(x)
1 + x
for a number x.Math.log10(x)
Math.log2(x)
Math.max([x[, y[, …]]])
Math.min([x[, y[, …]]])
Math.pow(x, y)
baseexponent
.Math.random()
Math.round(x)
Math.sign(x)
Math.sin(x)
Math.sinh(x)
Math.sqrt(x)
Math.tan(x)
Math.tanh(x)
Math.toSource()
"Math"
.Math.trunc(x)
Math
objectAs 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 do not 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) { var result = Math.gcd(arguments[0], arguments[1]); for (var 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`
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 |
Desktop | ||||||
---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
E |
Yes | Yes | 1 | Yes | Yes | Yes |
LN2 |
Yes | Yes | 1 | Yes | Yes | Yes |
LN10 |
Yes | Yes | 1 | Yes | Yes | Yes |
LOG2E |
Yes | Yes | 1 | Yes | Yes | Yes |
LOG10E |
Yes | Yes | 1 | Yes | Yes | Yes |
PI |
Yes | Yes | 1 | Yes | Yes | Yes |
SQRT1_2 |
Yes | Yes | 1 | Yes | Yes | Yes |
SQRT2 |
Yes | Yes | 1 | Yes | Yes | Yes |
abs |
Yes | Yes | 1 | Yes | Yes | Yes |
acos |
Yes | Yes | 1 | Yes | Yes | Yes |
acosh |
38 | Yes | 25 | No | 25 | 8 |
asin |
Yes | Yes | 1 | Yes | Yes | Yes |
asinh |
38 | Yes | 25 | No | 25 | 8 |
atan |
Yes | Yes | 1 | Yes | Yes | Yes |
atan2 |
Yes | Yes | 1 | Yes | Yes | Yes |
atanh |
38 | Yes | 25 | No | 25 | 8 |
cbrt |
38 | Yes | 25 | No | 25 | 8 |
ceil |
Yes | Yes | 1 | Yes | Yes | Yes |
clz32 |
38 | Yes | 31 | No | 25 | Yes |
cos |
Yes | Yes | 1 | Yes | Yes | Yes |
cosh |
38 | Yes | 25 | No | 25 | 8 |
exp |
Yes | Yes | 1 | Yes | Yes | Yes |
expm1 |
38 | Yes | 25 | No | 25 | 8 |
floor |
Yes | Yes | 1 | Yes | Yes | Yes |
fround |
38 | Yes | 26 | No | 25 | 8 |
hypot |
38 | Yes | 27 | No | 25 | 8 |
imul |
28 | Yes | 20 | No | 16 | 7 |
log |
Yes | Yes | 1 | Yes | Yes | Yes |
log1p |
38 | Yes | 25 | No | 25 | 8 |
log2 |
38 | Yes | 25 | No | 25 | 8 |
log10 |
38 | Yes | 25 | No | 25 | 8 |
max |
Yes | Yes | 1 | Yes | Yes | Yes |
min |
Yes | Yes | 1 | Yes | Yes | Yes |
pow |
Yes | Yes | 1 | Yes | Yes | Yes |
random |
Yes | Yes | 1 | Yes | Yes | Yes |
round |
Yes | Yes | 1 | Yes | Yes | Yes |
sign |
38 | Yes | 25 | No | 25 | 9 |
sin |
Yes | Yes | 1 | Yes | Yes | Yes |
sinh |
38 | Yes | 25 | No | 25 | 8 |
sqrt |
Yes | Yes | 1 | Yes | Yes | Yes |
tan |
Yes | Yes | 1 | Yes | Yes | Yes |
tanh |
38 | Yes | 25 | No | 25 | 8 |
trunc |
38 | Yes | 25 | No | 25 | 8 |
Mobile | |||||||
---|---|---|---|---|---|---|---|
Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
E |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
LN2 |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
LN10 |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
LOG2E |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
LOG10E |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
PI |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
SQRT1_2 |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
SQRT2 |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
abs |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
acos |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
acosh |
Yes | Yes | Yes | 25 | Yes | 8 | Yes |
asin |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
asinh |
Yes | Yes | Yes | 25 | Yes | 8 | Yes |
atan |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
atan2 |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
atanh |
Yes | Yes | Yes | 25 | Yes | 8 | Yes |
cbrt |
Yes | Yes | Yes | 25 | Yes | 8 | Yes |
ceil |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
clz32 |
Yes | Yes | Yes | 31 | Yes | Yes | Yes |
cos |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
cosh |
Yes | Yes | Yes | 25 | Yes | 8 | Yes |
exp |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
expm1 |
Yes | Yes | Yes | 25 | Yes | 8 | Yes |
floor |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
fround |
Yes | Yes | Yes | 26 | Yes | 8 | Yes |
hypot |
Yes | Yes | Yes | 27 | Yes | 8 | Yes |
imul |
Yes | Yes | Yes | 20 | Yes | 7 | Yes |
log |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
log1p |
Yes | Yes | Yes | 25 | Yes | 8 | Yes |
log2 |
Yes | Yes | Yes | 25 | Yes | 8 | Yes |
log10 |
Yes | Yes | Yes | 25 | Yes | 8 | Yes |
max |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
min |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
pow |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
random |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
round |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
sign |
Yes | Yes | Yes | 25 | Yes | Yes | Yes |
sin |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
sinh |
Yes | Yes | Yes | 25 | Yes | 8 | Yes |
sqrt |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
tan |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
tanh |
Yes | Yes | Yes | 25 | Yes | 8 | Yes |
trunc |
Yes | Yes | Yes | 25 | Yes | 8 | Yes |
Server | |
---|---|
Node.js | |
E |
Yes |
LN2 |
Yes |
LN10 |
Yes |
LOG2E |
Yes |
LOG10E |
Yes |
PI |
Yes |
SQRT1_2 |
Yes |
SQRT2 |
Yes |
abs |
Yes |
acos |
Yes |
acosh |
0.12 |
asin |
Yes |
asinh |
0.12 |
atan |
Yes |
atan2 |
Yes |
atanh |
0.12 |
cbrt |
0.12 |
ceil |
Yes |
clz32 |
0.12 |
cos |
Yes |
cosh |
0.12 |
exp |
Yes |
expm1 |
0.12 |
floor |
Yes |
fround |
0.12 |
hypot |
0.12 |
imul |
0.12 |
log |
Yes |
log1p |
0.12 |
log2 |
0.12 |
log10 |
0.12 |
max |
Yes |
min |
Yes |
pow |
Yes |
random |
Yes |
round |
Yes |
sign |
0.12 |
sin |
Yes |
sinh |
0.12 |
sqrt |
Yes |
tan |
Yes |
tanh |
0.12 |
trunc |
0.12 |
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math