Browser/User Agent Support
| IE | Mozilla | Netscape | Opera | Safari | 3.0+ | 1.0+ | 2.0+ | 7.0+ | 1.0+ |
|---|
Properties
| Property | IE | Mozilla | Netscape | Opera | Safari |
|---|---|---|---|---|---|
Euler's constant and the base of natural logarithms,
approximately 2.718.
| 3.0+ | 1.0+ | 2.0+ | 7.0+ | 1.0+ |
The natural logarithm of 10, approximately 2.302. | 3.0+ | 1.0+ | 2.0+ | 7.0+ | 1.0+ |
The natural logarithm of 2, approximately 0.693. | 3.0+ | 1.0+ | 2.0+ | 7.0+ | 1.0+ |
The base 10 logarithm of E (approximately 0.434). | 3.0+ | 1.0+ | 2.0+ | 7.0+ | 1.0+ |
The base 2 logarithm of E (approximately 1.442). | 3.0+ | 1.0+ | 2.0+ | 7.0+ | 1.0+ |
The ratio of the circumference of a circle to its
diameter, approximately 3.14159.
| 3.0+ | 1.0+ | 2.0+ | 7.0+ | 1.0+ |
The square root of 1/2; equivalently, 1 over the
square root of 2, approximately 0.707.
| 3.0+ | 1.0+ | 2.0+ | 7.0+ | 1.0+ |
The square root of 2, approximately 1.414. | 3.0+ | 1.0+ | 2.0+ | 7.0+ | 1.0+ |
Methods
| Method | IE | Mozilla | Netscape | Opera | Safari |
|---|---|---|---|---|---|
Returns the absolute value of a number. | 3.0+ | 1.0+ | 2.0+ | 7.0+ | 1.0+ |
Returns the arccosine (in radians) of a number. | 3.0+ | 1.0+ | 2.0+ | 7.0+ | 1.0+ |
Returns the arcsine (in radians) of a number. | 3.0+ | 1.0+ | 2.0+ | 7.0+ | 1.0+ |
Returns the arctangent (in radians) of a number. | 3.0+ | 1.0+ | 2.0+ | 7.0+ | 1.0+ |
Returns the arctangent of the quotient of its
arguments.
| 3.0+ | 1.0+ | 2.0+ | 7.0+ | 1.0+ |
Returns the smallest integer greater than or equal to
a number.
| 3.0+ | 1.0+ | 2.0+ | 7.0+ | 1.0+ |
Returns the cosine of a number. | 3.0+ | 1.0+ | 2.0+ | 7.0+ | 1.0+ |
Returns Ex, where
x is the argument, and E is
Euler's constant, the base of the natural logarithms.
| 3.0+ | 1.0+ | 2.0+ | 7.0+ | 1.0+ |
Returns the largest integer less than or equal to a
number.
| 3.0+ | 1.0+ | 2.0+ | 7.0+ | 1.0+ |
Returns the natural logarithm (base E) of
a number.
| 3.0+ | 1.0+ | 2.0+ | 7.0+ | 1.0+ |
Returns the larger of two numbers. | 3.0+ | 1.0+ | 2.0+ | 7.0+ | 1.0+ |
Returns the smaller of two numbers. | 3.0+ | 1.0+ | 2.0+ | 7.0+ | 1.0+ |
Returns base to the exponent power, that is, baseexponent. | 3.0+ | 1.0+ | 2.0+ | 7.0+ | 1.0+ |
Returns a pseudo-random number in the range [0,1)
-- that is, between 0 (inclusive) and 1 (exclusive).
The random number generator is seeded from the current
time, as in Java.
| 3.0+ | 1.0+ | 3.0+ | 7.0+ | 1.0+ |
Returns the value of a number rounded to the nearest
integer.
| 3.0+ | 1.0+ | 2.0+ | 7.0+ | 1.0+ |
Returns the sine of a number. | 3.0+ | 1.0+ | 2.0+ | 7.0+ | 1.0+ |
Returns the square root of a number. | 3.0+ | 1.0+ | 2.0+ | 7.0+ | 1.0+ |
Returns the tangent of a number. | 3.0+ | 1.0+ | 2.0+ | 7.0+ | 1.0+ |
Remarks
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.
It is often convenient to use the with
statement when a section of code uses several
Math constants and methods, so you don't
have to type "Math" repeatedly. For example,
with (Math) {
a = PI * r*r
y = r*sin(theta)
x = r*cos(theta)
}
References
Availability
JavaScript 1.0 | JScript 1.0 | ECMAScript v1
Property Detail
static Number E
Euler's constant and the base of natural logarithms, approximately 2.718.
-
Using
Math.EThe following function returns Euler's constant:
function getEuler() { return Math.E } - Remarks
-
Because
Eis a static property ofMath, you always use it asMath.E, rather than as a property of aMathobject you created. - Availability
JavaScript 1.0 | JScript 1.0 | ECMAScript v1
static Number LN10
The natural logarithm of 10, approximately 2.302.
-
Using
Math.LN10The following function returns the natural log of 10:
function getNatLog10() { return Math.LN10 } - Remarks
-
Because
LN10is a static property ofMath, you always use it asMath.LN10, rather than as a property of aMathobject you created. - Availability
JavaScript 1.0 | JScript 1.0 | ECMAScript v1
static Number LN2
The natural logarithm of 2, approximately 0.693.
-
Using
Math.LN2The following function returns the natural log of 2:
function getNatLog2() { return Math.LN2 } - Remarks
-
Because
LN2is a static property ofMath, you always use it asMath.LN2, rather than as a property of aMathobject you created. - Availability
JavaScript 1.0 | JScript 1.0 | ECMAScript v1
static Number LOG10E
The base 10 logarithm of E (approximately 0.434).
-
Using
Math.LOG10EThe following function returns the base 10 logarithm of
E:function getLog10e() { return Math.LOG10E } - Remarks
-
Because
LOG10Eis a static property ofMath, you always use it asMath.LOG10E, rather than as a property of aMathobject you created. - Availability
JavaScript 1.0 | JScript 1.0 | ECMAScript v1
static Number LOG2E
The base 2 logarithm of E (approximately 1.442).
-
Using
Math.LOG2EThe following function returns the base 2 logarithm of
E:function getLog2e() { return Math.LOG2E } - Remarks
-
Because
LOG2Eis a static property ofMath, you always use it asMath.LOG2E, rather than as a property of aMathobject you created. - Availability
JavaScript 1.0 | JScript 1.0 | ECMAScript v1
static Number PI
The ratio of the circumference of a circle to its diameter, approximately 3.14159.
-
Using
PIThe following function returns the value of pi:
function getPi() { return Math.PI } - Remarks
-
Because
PIis a static property ofMath, you always use it asMath.PI, rather than as a property of aMathobject you created. - Availability
JavaScript 1.0 | JScript 1.0 | ECMAScript v1
static Number SQRT1_2
The square root of 1/2; equivalently, 1 over the square root of 2, approximately 0.707.
-
Using
SQRT1_2The following function returns 1 over the square root of 2:
function getRoot1_2() { return Math.SQRT1_2 } - Remarks
-
Because
SQRT1_2is a static property ofMath, you always use it asMath.SQRT1_2, rather than as a property of aMathobject you created. - Availability
JavaScript 1.0 | JScript 1.0 | ECMAScript v1
static Number SQRT2
The square root of 2, approximately 1.414.
-
Using
Math.SQRT2The following function returns the square root of 2:
function getRoot2() { return Math.SQRT2 } - Remarks
-
Because
SQRT2is a static property ofMath, you always use it asMath.SQRT2, rather than as a property of aMathobject you created. - Availability
JavaScript 1.0 | JScript 1.0 | ECMAScript v1
Method Detail
abs(Number x) : static Number
Returns the absolute value of a number.
| Number | x | A number. |
-
Using
Math.absThe following function returns the absolute value of the variable
x:function getAbs(x) { return Math.abs(x) } - Remarks
-
absis a static method ofMath, so you always use it asMath.abs(), rather than as a method of aMathobject you created. - Availability
JavaScript 1.0 | JScript 1.0 | ECMAScript v1
acos(Number x) : static Number
Returns the arccosine (in radians) of a number.
| Number | x | A number. |
-
Using
Math.acosThe following function returns the arccosine of the variable
x:function getAcos(x) { return Math.acos(x) }If you pass -1 to
getAcos, it returns 3.141592653589793; if you pass 2, it returnsNaNbecause 2 is out of range. - Remarks
-
The
acosmethod returns a numeric value between 0 and pi radians for x between -1 and 1. If the value ofnumberis outside this range, it returnsNaN.acosis a static method ofMath, so you always use it asMath.acos(), rather than as a method of aMathobject you created. - See Also
- Availability
JavaScript 1.0 | JScript 1.0 | ECMAScript v1
asin(Number x) : static Number
Returns the arcsine (in radians) of a number.
| Number | x | A number. |
-
Using
Math.asinThe following function returns the arcsine of the variable
x:function getAsin(x) { return Math.asin(x) }If you pass
getAsinthe value 1, it returns 1.570796326794897 (pi/2); if you pass it the value 2, it returnsNaNbecause 2 is out of range. - Remarks
-
The
asinmethod returns a numeric value between -pi/2 and pi/2 radians. If the value ofnumberis outside this range, it returnsNaN.asinis a static method ofMath, so you always use it asMath.asin(), rather than as a method of aMathobject you created. - See Also
- Availability
JavaScript 1.0 | JScript 1.0 | ECMAScript v1
atan(Number x) : static Number
Returns the arctangent (in radians) of a number.
| Number | x | A number. |
-
Using
Math.atanThe following function returns the arctangent of the variable
x:function getAtan(x) { return Math.atan(x) }If you pass
getAtanthe value 1, it returns 0.7853981633974483; if you pass it the value .5, it returns 0.4636476090008061. - Remarks
-
The
atanmethod returns a numeric value between -pi/2 and pi/2 radians.atanis a static method ofMath, so you always use it asMath.atan(), rather than as a method of aMathobject you created. - See Also
- Availability
JavaScript 1.0 | JScript 1.0 | ECMAScript v1
atan2(Object y, Object x) : static Number
Returns the arctangent of the quotient of its arguments.
| Object | y | Number. |
| Object | x | Number. |
-
Using
Math.atan2The following function returns the angle of the polar coordinate:
function getAtan2(x,y) { return Math.atan2(x,y) }If you pass
getAtan2the values (90,15), it returns 1.4056476493802699; if you pass it the values (15,90), it returns 0.16514867741462683. - Remarks
-
The
atan2method returns a numeric value between -pi and pi representing the angle theta of an (x,y) point. This is the counterclockwise angle, measured in radians, between the positive X axis, and the point (x,y). Note that the arguments to this function pass the y-coordinate first and the x-coordinate second.atan2is passed separatexandyarguments, andatanis passed the ratio of those two arguments.Because
atan2is a static method ofMath, you always use it asMath.atan2(), rather than as a method of aMathobject you created. - See Also
- Availability
JavaScript 1.0 | JScript 1.0 | ECMAScript v1
ceil(Number x) : static Number
Returns the smallest integer greater than or equal to a number.
| Number | x | A number. |
-
Using
Math.ceilThe following function returns the ceil value of the variable
x:function getCeil(x) { return Math.ceil(x) }If you pass 45.95 to
getCeil, it returns 46; if you pass -45.95, it returns -45. - Remarks
-
Because
ceilis a static method ofMath, you always use it asMath.ceil(), rather than as a method of aMathobject you created. - See Also
- Availability
JavaScript 1.0 | JScript 1.0 | ECMAScript v1
cos(Number x) : static Number
Returns the cosine of a number.
| Number | x | A number. |
-
Using
Math.cosThe following function returns the cosine of the variable
x:function getCos(x) { return Math.cos(x) }If
xequals 2*Math.PI,getCosreturns 1; ifxequalsMath.PI, thegetCosmethod returns -1. - Remarks
-
The
cosmethod returns a numeric value between -1 and 1, which represents the cosine of the angle.Because
cosis a static method ofMath, you always use it asMath.cos(), rather than as a method of aMathobject you created. - See Also
- Availability
JavaScript 1.0 | JScript 1.0 | ECMAScript v1
exp(Number x) : static Number
Returns Ex, where x is the argument, and E is Euler's constant, the base of the natural logarithms.
| Number | x | A number. |
-
Using
Math.expThe following function returns the exponential value of the variable
x:function getExp(x) { return Math.exp(x) }If you pass
getExpthe value 1, it returns 2.718281828459045. - Remarks
-
Because
expis a static method ofMath, you always use it asMath.exp(), rather than as a method of aMathobject you created. - See Also
- Availability
JavaScript 1.0 | JScript 1.0 | ECMAScript v1
floor(Number x) : static Number
Returns the largest integer less than or equal to a number.
| Number | x | A number. |
-
Using
Math.floorThe following function returns the floor value of the variable
x:function getFloor(x) { return Math.floor(x) }If you pass 45.95 to
getFloor, it returns 45; if you pass -45.95, it returns -46. - Remarks
-
Because
flooris a static method ofMath, you always use it asMath.floor(), rather than as a method of aMathobject you created. - See Also
- Availability
JavaScript 1.0 | JScript 1.0 | ECMAScript v1
log(Number x) : static Number
Returns the natural logarithm (base E) of a number.
| Number | x | A number. |
-
Using
Math.logThe following function returns the natural log of the variable
x:function getLog(x) { return Math.log(x) }If you pass
getLogthe value 10, it returns 2.302585092994046; if you pass it the value 0, it returns-Infinity; if you pass it the value -1, it returnsNaNbecause -1 is out of range. - Remarks
-
If the value of
numberis negative, the return value is alwaysNaN.Because
logis a static method ofMath, you always use it asMath.log(), rather than as a method of aMathobject you created. - See Also
- Availability
JavaScript 1.0 | JScript 1.0 | ECMAScript v1
max(Number x, Number y) : static Number
Returns the larger of two numbers.
| Number | x | Number. |
| Number | y | Number. |
-
Using
Math.maxThe following function evaluates the variables
xandy:function getMax(x,y) { return Math.max(x,y) }If you pass
getMaxthe values 10 and 20, it returns 20; if you pass it the values -10 and -20, it returns -10. - Remarks
-
Because
maxis a static method ofMath, you always use it asMath.max(), rather than as a method of aMathobject you created. - See Also
- Availability
JavaScript 1.0 | JScript 1.0 | ECMAScript v1 | enhanced in ECMAScript v3
min() : static Number
Returns the smaller of two numbers.
-
Using
Math.minThe following function evaluates the variables
xandy:function getMin(x,y) { return Math.min(x,y) }If you pass
getMinthe values 10 and 20, it returns 10; if you pass it the values -10 and -20, it returns -20. - Remarks
-
Because
minis a static method ofMath, you always use it asMath.min(), rather than as a method of aMathobject you created. - See Also
- Availability
JavaScript 1.0 | JScript 1.0 | ECMAScript v1 | enhanced in ECMAScript v3
pow() : static Number
Returns base to the exponent power, that is, baseexponent.
-
Using
Math.powfunction raisePower(x,y) { return Math.pow(x,y) }If
xis 7 andyis 2, raisePower returns 49 (7 to the power of 2). - Remarks
-
Because
powis a static method ofMath, you always use it asMath.pow(), rather than as a method of aMathobject you created. - See Also
- Availability
JavaScript 1.0 | JScript 1.0 | ECMAScript v1
random() : static Number
Returns a pseudo-random number in the range [0,1) -- that is, between 0 (inclusive) and 1 (exclusive). The random number generator is seeded from the current time, as in Java.
-
Using
Math.random//Returns a random number between 0 (inclusive) and 1 (exclusive) function getRandom() { return Math.random() } - Remarks
-
Because
randomis a static method ofMath, you always use it asMath.random(), rather than as a method of aMathobject you created. - Availability
JavaScript 1.1 | JScript 1.0 | ECMAScript v1
round(Number x) : static Number
Returns the value of a number rounded to the nearest integer.
| Number | x | A number. |
-
Using
Math.round//Returns the value 20 x=Math.round(20.49) //Returns the value 21 x=Math.round(20.5) //Returns the value -20 x=Math.round(-20.5) //Returns the value -21 x=Math.round(-20.51)
- Remarks
-
If the fractional portion of
numberis .5 or greater, the argument is rounded to the next higher integer. If the fractional portion ofnumberis less than .5, the argument is rounded to the next lower integer.Because
roundis a static method ofMath, you always use it asMath.round(), rather than as a method of aMathobject you created. - Availability
JavaScript 1.0 | JScript 1.0 | ECMAScript v1
sin(Number x) : static Number
Returns the sine of a number.
| Number | x | A number. |
-
Using
Math.sinThe following function returns the sine of the variable
x:function getSine(x) { return Math.sin(x) }If you pass
getSinethe valueMath.PI/2, it returns 1. - Remarks
-
The
sinmethod returns a numeric value between -1 and 1, which represents the sine of the argument.Because
sinis a static method ofMath, you always use it asMath.sin(), rather than as a method of aMathobject you created. - See Also
- Availability
JavaScript 1.0 | JScript 1.0 | ECMAScript v1
sqrt(Number x) : static Number
Returns the square root of a number.
| Number | x | A number. |
-
Using
Math.sqrtThe following function returns the square root of the variable
x:function getRoot(x) { return Math.sqrt(x) }If you pass
getRootthe value 9, it returns 3; if you pass it the value 2, it returns 1.414213562373095. - Remarks
-
If the value of
numberis negative,sqrtreturnsNaN.Because
sqrtis a static method ofMath, you always use it asMath.sqrt(), rather than as a method of aMathobject you created. - Availability
JavaScript 1.0 | JScript 1.0 | ECMAScript v1
tan(Number x) : static Number
Returns the tangent of a number.
| Number | x | A number. |
-
Using
Math.tanThe following function returns the tangent of the variable
x:function getTan(x) { return Math.tan(x) } - Remarks
-
The
tanmethod returns a numeric value that represents the tangent of the angle.Because
tanis a static method ofMath, you always use it asMath.tan(), rather than as a method of aMathobject you created. - See Also
- Availability
JavaScript 1.0 | JScript 1.0 | ECMAScript v1
