The JavaScript Math Object

The JavaScript Math object is a global object.

Math is not a constructor. Math is a static object, meaning that it's properties and methods are static and are accessible without an instance of the object.

For example, to get the absolute value of a number one needs to call the Math.abs() function. Creating an instance of the Math objects results in an error:

var my_math = new Math(); // wrong, throws an TypeError
Math.abs(-123); // 123

Likewise, you refer to the constant pi as Math.PI.

Math Object Properties

E
Mathematical constant e, the base of the natural logarithm, also known as Euler's number (2.718)
LN2
Natural logarithm of 2 (0.693)
LN10
Natural logarithm of 10 (2.302)
LOG2E
Base 2 logarithm of E (1.442)
LOG10E
Base 10 logarithm of E (0.434)
PI
Mathematical constant pi, the ratio of a circle's circumference to its diameter (3.141593)
SQRT1_2
Square root of 1 / 2, equivalent to 1 / Math.sqrt(2) (0.707)
SQRT2
Square root of 2, equivalent to Math.sqrt(2) (1.414)

Math Object Methods

abs(number)
Returns the absolute value of a number
acos(number)
Returns the arccosine of a number in radians. Complementary function of Math.cos()
asin(number)
Returns the arcsine of a number in radians
atan(number)
Returns the arctangent or a number in radians
atan2(y, x)
Returns the arctangent of the quotient of its arguments, similar to calculating the arctangent of y/x
ceil(number)
Returns the next smallest integer by rounding up the number if necessary
cos(number)
Returns the cosine of a number
exp(e)
Returns e (Math.E) raised to the power of x, equivalent to Math.pow(Math.E, x)
floor(number)
Returns the previous largest integer by rounding down the number if necessary
log(x)
Returns the natural logarithm of x
max([value1 [ , value2 [ , ...]]])
Returns the largest of zero or more values
min([value1 [ , value2 [ , ...]]])
Returns the smallest of zero or more values
pow(base, exponent)
Returns base raised to the power of exponent
random()
Generates a random value between 0 and 1
round(number)
Rounds a number to the nearest integer
sin(number)
Returns the sine of a number
sqrt(number)
Returns the square root of a number
tan(number)
Returns the tangent of a number

If you see a typo, want to make a suggestion or have anything in particular you'd like to know more about, please drop us an e-mail at hello at diveintojavascript dot com.

Copyright © 2010-2013 Dive Into JavaScript