Math
is a built-in object that has properties and methods for mathematical constants and functions. Itβs not a function object.
Math
works with the Number
type. It doesn't work with BigInt
.
Description
Unlike many 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.
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!
Static 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); approximately
0.707
. Math.SQRT2
- Square root of
2
; approximately1.414
.
Static methods
Math.abs(x)
- Returns the absolute value of
x
. Math.acos(x)
- Returns the arccosine of
x
. Math.acosh(x)
- Returns the hyperbolic arccosine of
x
. Math.asin(x)
- Returns the arcsine of
x
. Math.asinh(x)
- Returns the hyperbolic arcsine of a number.
Math.atan(x)
- Returns the arctangent of
x
. Math.atanh(x)
- Returns the hyperbolic arctangent of
x
. Math.atan2(y, x)
- Returns the arctangent of the quotient of its arguments.
Math.cbrt(x)
- Returns the cube root of
x
. Math.ceil(x)
- Returns the smallest integer greater than or equal to
x
. Math.clz32(x)
- Returns the number of leading zeroes of the 32-bit integer
x
. Math.cos(x)
- Returns the cosine of
x
. Math.cosh(x)
- Returns the hyperbolic cosine of
x
. Math.exp(x)
- Returns
Ex
, wherex
is the argument, andE
is Euler's constant (2.718
β¦, the base of the natural logarithm). Math.expm1(x)
- Returns subtracting
1
fromexp(x)
. Math.floor(x)
- Returns the largest integer less than or equal to
x
. Math.fround(x)
- Returns the nearest single precision float representation of
x
. Math.hypot([x[, y[, β¦]]])
- Returns the square root of the sum of squares of its arguments.
Math.imul(x, y)
- Returns the result of the 32-bit integer multiplication of
x
andy
. Math.log(x)
- Returns the natural logarithm (γe; also, γ) of
x
. Math.log1p(x)
- Returns the natural logarithm (γe; also γ) of
1 + x
for the numberx
. Math.log10(x)
- Returns the base-10 logarithm of
x
. Math.log2(x)
- Returns the base-2 logarithm of
x
. 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
x
to the exponent powery
(that is,xy
). Math.random()
- Returns a pseudo-random number between
0
and1
. Math.round(x)
- Returns the value of the number
x
rounded to the nearest integer. Math.sign(x)
- Returns the sign of the
x
, indicating whetherx
is positive, negative, or zero. Math.sin(x)
- Returns the sine of
x
. Math.sinh(x)
- Returns the hyperbolic sine of
x
. Math.sqrt(x)
- Returns the positive square root of
x
. Math.tan(x)
- Returns the tangent of
x
. Math.tanh(x)
- Returns the hyperbolic tangent of
x
. Math.trunc(x)
- Returns the integer portion of
x
, removing any fractional digits.
Examples
Converting between degrees and radians
The trigonometric functions sin()
, cos()
, tan()
, asin()
, acos()
, atan()
, and atan2()
expect (and return) angles in radians.
Since humans tend to think in degrees, and some functions (such as CSS transforms) can accept degrees, it is a good idea to keep functions handy that convert between the two:
function degToRad(degrees) { return degrees * (Math.PI / 180); }; function radToDeg(rad) { return rad / (Math.PI / 180); };
Calculating the height of an equalateral triangle
If we want to calculate the height of an equalateral triangle, and we know its side length is 100, we can use the formulae length of the adjacent multiplied by the tangent of the angle is equal to the opposite.
In JavaScript, we can do this with the following:
50 * Math.tan(degToRad(60)).
We use our degToRad()
function to convert 60 degrees to radians, as Math.tan()
expects an input value in radians.
Returning a random integer between two bounds
This can be achieved with a combination of Math.random()
and Math.floor()
:
function random(min, max) { const num = Math.floor(Math.random() * (max - min + 1)) + min; return num; } random(1, 10);
Specifications
Specification |
---|
ECMAScript (ECMA-262) The definition of 'Math' in that specification. |
Browser compatibility
Desktop | Mobile | Server | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
E | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 3 | Opera Full support 3 | Safari Full support 1 | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support 10.1 | Safari iOS Full support 1 | Samsung Internet Android Full support 1.0 | nodejs Full support 0.1.100 |
LN2 | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 3 | Opera Full support 3 | Safari Full support 1 | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support 10.1 | Safari iOS Full support 1 | Samsung Internet Android Full support 1.0 | nodejs Full support 0.1.100 |
LN10 | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 3 | Opera Full support 3 | Safari Full support 1 | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support 10.1 | Safari iOS Full support 1 | Samsung Internet Android Full support 1.0 | nodejs Full support 0.1.100 |
LOG2E | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 3 | Opera Full support 3 | Safari Full support 1 | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support 10.1 | Safari iOS Full support 1 | Samsung Internet Android Full support 1.0 | nodejs Full support 0.1.100 |
LOG10E | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 3 | Opera Full support 3 | Safari Full support 1 | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support 10.1 | Safari iOS Full support 1 | Samsung Internet Android Full support 1.0 | nodejs Full support 0.1.100 |
PI | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 3 | Opera Full support 3 | Safari Full support 1 | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support 10.1 | Safari iOS Full support 1 | Samsung Internet Android Full support 1.0 | nodejs Full support 0.1.100 |
SQRT1_2 | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 3 | Opera Full support 3 | Safari Full support 1 | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support 10.1 | Safari iOS Full support 1 | Samsung Internet Android Full support 1.0 | nodejs Full support 0.1.100 |
SQRT2 | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 3 | Opera Full support 3 | Safari Full support 1 | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support 10.1 | Safari iOS Full support 1 | Samsung Internet Android Full support 1.0 | nodejs Full support 0.1.100 |
abs | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 3 | Opera Full support 3 | Safari Full support 1 | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support 10.1 | Safari iOS Full support 1 | Samsung Internet Android Full support 1.0 | nodejs Full support 0.1.100 |
acos | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 3 | Opera Full support 3 | Safari Full support 1 | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support 10.1 | Safari iOS Full support 1 | Samsung Internet Android Full support 1.0 | nodejs Full support 0.1.100 |
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 38 | Chrome Android Full support 38 | Firefox Android Full support 25 | Opera Android Full support 25 | Safari iOS Full support 8 | Samsung Internet Android Full support 3.0 | 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 3 | Safari Full support 1 | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support 10.1 | Safari iOS Full support 1 | Samsung Internet Android Full support 1.0 | nodejs Full support 0.1.100 |
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 38 | Chrome Android Full support 38 | Firefox Android Full support 25 | Opera Android Full support 25 | Safari iOS Full support 8 | Samsung Internet Android Full support 3.0 | 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 3 | Safari Full support 1 | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support 10.1 | Safari iOS Full support 1 | Samsung Internet Android Full support 1.0 | nodejs Full support 0.1.100 |
atan2 | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 4 | Opera Full support 3 | Safari Full support 1 | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support 10.1 | Safari iOS Full support 1 | Samsung Internet Android Full support 1.0 | nodejs Full support 0.1.100 |
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 38 | Chrome Android Full support 38 | Firefox Android Full support 25 | Opera Android Full support 25 | Safari iOS Full support 8 | Samsung Internet Android Full support 3.0 | 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 38 | Chrome Android Full support 38 | Firefox Android Full support 25 | Opera Android Full support 25 | Safari iOS Full support 8 | Samsung Internet Android Full support 3.0 | 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 3 | Safari Full support 1 | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support 10.1 | Safari iOS Full support 1 | Samsung Internet Android Full support 1.0 | nodejs Full support 0.1.100 |
clz32 | Chrome Full support 38 | Edge Full support 12 | Firefox Full support 31 | IE No support No | Opera Full support 25 | Safari Full support 7 | WebView Android Full support 38 | Chrome Android Full support 38 | Firefox Android Full support 31 | Opera Android Full support 25 | Safari iOS Full support 7 | Samsung Internet Android Full support 3.0 | 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 3 | Safari Full support 1 | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support 10.1 | Safari iOS Full support 1 | Samsung Internet Android Full support 1.0 | nodejs Full support 0.1.100 |
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 38 | Chrome Android Full support 38 | Firefox Android Full support 25 | Opera Android Full support 25 | Safari iOS Full support 8 | Samsung Internet Android Full support 3.0 | 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 3 | Safari Full support 1 | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support 10.1 | Safari iOS Full support 1 | Samsung Internet Android Full support 1.0 | nodejs Full support 0.1.100 |
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 38 | Chrome Android Full support 38 | Firefox Android Full support 25 | Opera Android Full support 25 | Safari iOS Full support 8 | Samsung Internet Android Full support 3.0 | 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 3 | Safari Full support 1 | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support 10.1 | Safari iOS Full support 1 | Samsung Internet Android Full support 1.0 | nodejs Full support 0.1.100 |
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 38 | Chrome Android Full support 38 | Firefox Android Full support 26 | Opera Android Full support 25 | Safari iOS Full support 8 | Samsung Internet Android Full support 3.0 | 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 38 | Chrome Android Full support 38 | Firefox Android Full support 27 | Opera Android Full support 25 | Safari iOS Full support 8 | Samsung Internet Android Full support 3.0 | 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 β€37 | Chrome Android Full support 28 | Firefox Android Full support 20 | Opera Android Full support 15 | Safari iOS Full support 7 | Samsung Internet Android Full support 1.5 | 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 3 | Safari Full support 1 | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support 10.1 | Safari iOS Full support 1 | Samsung Internet Android Full support 1.0 | nodejs Full support 0.1.100 |
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 38 | Chrome Android Full support 38 | Firefox Android Full support 25 | Opera Android Full support 25 | Safari iOS Full support 8 | Samsung Internet Android Full support 3.0 | 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 38 | Chrome Android Full support 38 | Firefox Android Full support 25 | Opera Android Full support 25 | Safari iOS Full support 8 | Samsung Internet Android Full support 3.0 | 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 38 | Chrome Android Full support 38 | Firefox Android Full support 25 | Opera Android Full support 25 | Safari iOS Full support 8 | Samsung Internet Android Full support 3.0 | 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 3 | Safari Full support 1 | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support 10.1 | Safari iOS Full support 1 | Samsung Internet Android Full support 1.0 | nodejs Full support 0.1.100 |
min | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 3 | Opera Full support 3 | Safari Full support 1 | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support 10.1 | Safari iOS Full support 1 | Samsung Internet Android Full support 1.0 | nodejs Full support 0.1.100 |
pow | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 3 | Opera Full support 3 | Safari Full support 1 | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support 10.1 | Safari iOS Full support 1 | Samsung Internet Android Full support 1.0 | nodejs Full support 0.1.100 |
random | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 3 | Opera Full support 3 | Safari Full support 1 | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support 10.1 | Safari iOS Full support 1 | Samsung Internet Android Full support 1.0 | nodejs Full support 0.1.100 |
round | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 3 | Opera Full support 3 | Safari Full support 1 | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support 10.1 | Safari iOS Full support 1 | Samsung Internet Android Full support 1.0 | nodejs Full support 0.1.100 |
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 38 | Chrome Android Full support 38 | Firefox Android Full support 25 | Opera Android Full support 25 | Safari iOS Full support 9 | Samsung Internet Android Full support 3.0 | 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 3 | Safari Full support 1 | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support 10.1 | Safari iOS Full support 1 | Samsung Internet Android Full support 1.0 | nodejs Full support 0.1.100 |
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 38 | Chrome Android Full support 38 | Firefox Android Full support 25 | Opera Android Full support 25 | Safari iOS Full support 8 | Samsung Internet Android Full support 3.0 | 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 3 | Safari Full support 1 | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support 10.1 | Safari iOS Full support 1 | Samsung Internet Android Full support 1.0 | nodejs Full support 0.1.100 |
tan | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 3 | Opera Full support 3 | Safari Full support 1 | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support 10.1 | Safari iOS Full support 1 | Samsung Internet Android Full support 1.0 | nodejs Full support 0.1.100 |
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 38 | Chrome Android Full support 38 | Firefox Android Full support 25 | Opera Android Full support 25 | Safari iOS Full support 8 | Samsung Internet Android Full support 3.0 | 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 38 | Chrome Android Full support 38 | Firefox Android Full support 25 | Opera Android Full support 25 | Safari iOS Full support 8 | Samsung Internet Android Full support 3.0 | nodejs Full support 0.12 |
Legend
- Full support
- Full support
- No support
- No support