W3cubDocs

/JavaScript

Math

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; approximately 0.693.
Math.LN10
Natural logarithm of 10; approximately 2.303.
Math.LOG2E
Base-2 logarithm of E; approximately 1.443.
Math.LOG10E
Base-10 logarithm of E; approximately 0.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; approximately 1.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, where x is the argument, and E is Euler's constant (2.718…, the base of the natural logarithm).
Math.expm1(x)
Returns subtracting 1 from exp(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 and y.
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 number x.
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 power y (that is, xy).
Math.random()
Returns a pseudo-random number between 0 and 1.
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 whether x 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

Browser compatibilityUpdate compatibility data on GitHub

Desktop
Chrome Edge Firefox Internet Explorer Opera Safari
E 1 12 1 3 3 1
LN2 1 12 1 3 3 1
LN10 1 12 1 3 3 1
LOG2E 1 12 1 3 3 1
LOG10E 1 12 1 3 3 1
PI 1 12 1 3 3 1
SQRT1_2 1 12 1 3 3 1
SQRT2 1 12 1 3 3 1
abs 1 12 1 3 3 1
acos 1 12 1 3 3 1
acosh 38 12 25 No 25 8
asin 1 12 1 3 3 1
asinh 38 12 25 No 25 8
atan 1 12 1 3 3 1
atan2 1 12 1 4 3 1
atanh 38 12 25 No 25 8
cbrt 38 12 25 No 25 8
ceil 1 12 1 3 3 1
clz32 38 12 31 No 25 7
cos 1 12 1 3 3 1
cosh 38 12 25 No 25 8
exp 1 12 1 3 3 1
expm1 38 12 25 No 25 8
floor 1 12 1 3 3 1
fround 38 12 26 No 25 8
hypot 38 12 27 No 25 8
imul 28 12 20 No 16 7
log 1 12 1 3 3 1
log1p 38 12 25 No 25 8
log2 38 12 25 No 25 8
log10 38 12 25 No 25 8
max 1 12 1 3 3 1
min 1 12 1 3 3 1
pow 1 12 1 3 3 1
random 1 12 1 3 3 1
round 1 12 1 3 3 1
sign 38 12 25 No 25 9
sin 1 12 1 3 3 1
sinh 38 12 25 No 25 8
sqrt 1 12 1 3 3 1
tan 1 12 1 3 3 1
tanh 38 12 25 No 25 8
trunc 38 12 25 No 25 8
Mobile
Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
E 1 18 4 10.1 1 1.0
LN2 1 18 4 10.1 1 1.0
LN10 1 18 4 10.1 1 1.0
LOG2E 1 18 4 10.1 1 1.0
LOG10E 1 18 4 10.1 1 1.0
PI 1 18 4 10.1 1 1.0
SQRT1_2 1 18 4 10.1 1 1.0
SQRT2 1 18 4 10.1 1 1.0
abs 1 18 4 10.1 1 1.0
acos 1 18 4 10.1 1 1.0
acosh 38 38 25 25 8 3.0
asin 1 18 4 10.1 1 1.0
asinh 38 38 25 25 8 3.0
atan 1 18 4 10.1 1 1.0
atan2 1 18 4 10.1 1 1.0
atanh 38 38 25 25 8 3.0
cbrt 38 38 25 25 8 3.0
ceil 1 18 4 10.1 1 1.0
clz32 38 38 31 25 7 3.0
cos 1 18 4 10.1 1 1.0
cosh 38 38 25 25 8 3.0
exp 1 18 4 10.1 1 1.0
expm1 38 38 25 25 8 3.0
floor 1 18 4 10.1 1 1.0
fround 38 38 26 25 8 3.0
hypot 38 38 27 25 8 3.0
imul ≤37 28 20 15 7 1.5
log 1 18 4 10.1 1 1.0
log1p 38 38 25 25 8 3.0
log2 38 38 25 25 8 3.0
log10 38 38 25 25 8 3.0
max 1 18 4 10.1 1 1.0
min 1 18 4 10.1 1 1.0
pow 1 18 4 10.1 1 1.0
random 1 18 4 10.1 1 1.0
round 1 18 4 10.1 1 1.0
sign 38 38 25 25 9 3.0
sin 1 18 4 10.1 1 1.0
sinh 38 38 25 25 8 3.0
sqrt 1 18 4 10.1 1 1.0
tan 1 18 4 10.1 1 1.0
tanh 38 38 25 25 8 3.0
trunc 38 38 25 25 8 3.0
Server
Node.js
E 0.1.100
LN2 0.1.100
LN10 0.1.100
LOG2E 0.1.100
LOG10E 0.1.100
PI 0.1.100
SQRT1_2 0.1.100
SQRT2 0.1.100
abs 0.1.100
acos 0.1.100
acosh 0.12
asin 0.1.100
asinh 0.12
atan 0.1.100
atan2 0.1.100
atanh 0.12
cbrt 0.12
ceil 0.1.100
clz32 0.12
cos 0.1.100
cosh 0.12
exp 0.1.100
expm1 0.12
floor 0.1.100
fround 0.12
hypot 0.12
imul 0.12
log 0.1.100
log1p 0.12
log2 0.12
log10 0.12
max 0.1.100
min 0.1.100
pow 0.1.100
random 0.1.100
round 0.1.100
sign 0.12
sin 0.1.100
sinh 0.12
sqrt 0.1.100
tan 0.1.100
tanh 0.12
trunc 0.12

See also

© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://wiki.developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math