Math Object Overview
The Math object allows JavaScript to perform mathematical tasks.
The Math object does not have a constructor. All properties/methods of Math can be called by using Math as an object, without creating it.
The following examples show various ways to use the Math object:
// Returns PI
var x = Math.PI;
// Returns the square root of 16
var y = Math.sqrt(16);
Properties
Property | Description |
---|---|
E |
Returns Euler's number (approx. 2.718) |
LN2 |
Returns the natural logarithm of 2 (approx. 0.693) |
LN10 |
Returns the natural logarithm of 10 (approx. 2.302) |
LOG2E |
Returns the base-2 logarithm of E (approx. 1.442) |
LOG10E |
Returns the base-10 logarithm of E (approx. 0.434) |
PI |
Returns PI (approx. 3.14159) |
SQRT1_2 |
Returns the square root of 1/2 (approx. 0.707) |
SQRT2 |
Returns the square root of 2 (approx. 1.414) |
Methods
Method | Description |
---|---|
abs(x) |
Returns the absolute value of x |
acos(x) |
Returns the arccosine of x, in radians |
asin(x) |
Returns the arcsine of x, in radians |
atan(x) |
Returns the arctangent of x as a numeric value between -PI/2 and PI/2 radians |
atan2(y,x) |
Returns the arctangent of the quotient of its arguments |
ceil(x) |
Returns x, rounded upwards to the nearest integer |
cos(x) |
Returns the cosine of x (x is in radians) |
exp(x) |
Returns the value of Ex |
floor(x) |
Returns x, rounded downwards to the nearest integer |
log(x) |
Returns the natural logarithm (base E) of x |
max(x,y,z,...,n) |
Returns the number with the highest value |
min(x,y,z,...,n) |
Returns the number with the lowest value |
pow(x,y) |
Returns the value of x to the power of y |
random() |
Returns a random number between 0 and 1 |
round(x) |
Rounds x to the nearest integer |
sin(x) |
Returns the sine of x (x is in radians) |
sqrt(x) |
Returns the square root of x |
tan(x) |
Returns the tangent of an angle |
Description
The PI property returns the ratio of a circle's area to the square of its radius, approximately 3.14159.
This property has the form:
Math.PI
Examples
The following example shows the basic use of this property.
<script>
document.write("PI: " + Math.PI);
</script>
This produces the following result:
Browser Support
Firefox | IE | Chrome | Opera | Safari |
---|---|---|---|---|
Description
The PI property returns the ratio of a circle's area to the square of its radius, approximately 3.14159.
This property has the form:
Math.PI
Examples
The following example shows the basic use of this property.
<script>
document.write("PI: " + Math.PI);
</script>
This produces the following result:
Browser Support
Firefox | IE | Chrome | Opera | Safari |
---|---|---|---|---|
Description
The pow() method returns the value of x to the power of y (xy).
This method has the form:
Math.pow(x,y)
Parameter | Description |
---|---|
x | (required) The base |
y | (required) The exponent |
Examples
The following example shows the basic use of this method.
<script>
document.write(Math.pow(2,1));
document.write("<br />");
document.write(Math.pow(2,8));
</script>
This produces the following result:
Browser Support
Firefox | IE | Chrome | Opera | Safari |
---|---|---|---|---|
Description
The random() method returns a random number between 0 and 1.
This method has the form:
Math.random()
Examples
The following example shows the basic use of this method.
<script>
document.write(Math.random());
</script>
This produces the following result:
Browser Support
Firefox | IE | Chrome | Opera | Safari |
---|---|---|---|---|
Description
The round() method rounds a number to the nearest integer.
This method has the form:
Math.round(x)
Parameter | Description |
---|---|
x | (required) A number |
Examples
The following example shows the basic use of this method.
<script>
document.write(Math.round(123.45));
document.write("<br />");
document.write(Math.round(123.55));
</script>
This produces the following result:
Browser Support
Firefox | IE | Chrome | Opera | Safari |
---|---|---|---|---|
Description
The sin() method returns the sine of a number.
Note: This method returns a numeric value between -1 and 1, which represents the sine of the parameter x.
This method has the form:
Math.sin(x)
Parameter | Description |
---|---|
x | (required) A number |
Examples
The following example shows the basic use of this method.
<script>
document.write(Math.sin(0));
document.write("<br />");
document.write(Math.sin(Math.PI));
</script>
This produces the following result:
Browser Support
Firefox | IE | Chrome | Opera | Safari |
---|---|---|---|---|
Description
The sqrt() method returns the square root of a number.
This method has the form:
Math.sqrt(x)
Parameter | Description |
---|---|
x | (required) A number. If x is a negative number, NaN is returned |
Examples
The following example shows the basic use of this method.
<script>
document.write(Math.sqrt(9));
</script>
This produces the following result:
Browser Support
Firefox | IE | Chrome | Opera | Safari |
---|---|---|---|---|
Description
The SQRT1_2 property returns the square root of 1/2, approximately 0.707.
This property has the form:
Math.SQRT1_2
Examples
The following example shows the basic use of this property.
<script>
document.write("SQRT1_2: " + Math.SQRT1_2);
</script>
This produces the following result:
Browser Support
Firefox | IE | Chrome | Opera | Safari |
---|---|---|---|---|
Description
The SQRT2 property returns the square root of 2, approximately 1.414.
This property has the form:
Math.SQRT2
Examples
The following example shows the basic use of this property.
<script>
document.write("SQRT2: " + Math.SQRT2);
</script>
This produces the following result:
Browser Support
Firefox | IE | Chrome | Opera | Safari |
---|---|---|---|---|
Description
The abs() method returns the absolute value of a number.
This method has the form:
Math.abs(x)
Parameter | Description |
---|---|
x | (required) A number |
Examples
The following example shows the basic use of this method.
<script>
document.write(Math.abs(123.45));
document.write("<br />");
document.write(Math.abs(-123.45));
</script>
This produces the following result:
Browser Support
Firefox | IE | Chrome | Opera | Safari |
---|---|---|---|---|
Description
The acos() method returns the arccosine of a number as a numeric value value between 0 and PI radians.
Note: If the parameter x is outside the range -1 to 1, the browser will return NaN.
This method has the form:
Math.acos(x)
Parameter | Description |
---|---|
x | (required) A number |
Examples
The following example shows the basic use of this method.
<script>
document.write(Math.acos(0));
document.write("<br />");
document.write(Math.acos(-1));
document.write("<br />");
document.write(Math.acos(1));
</script>
This produces the following result:
Browser Support
Firefox | IE | Chrome | Opera | Safari |
---|---|---|---|---|
Description
The asin() method returns the arcsine of a number as a numeric value between -PI/2 and PI/2 radians.
Note: If the parameter x is outside the range -1 to 1, the browser will return NaN.
This method has the form:
Math.asin(x)
Parameter | Description |
---|---|
x | (required) A number |
Examples
The following example shows the basic use of this method.
<script>
document.write(Math.asin(0));
document.write("<br />");
document.write(Math.asin(-1));
document.write("<br />");
document.write(Math.asin(1));
</script>
This produces the following result:
Browser Support
Firefox | IE | Chrome | Opera | Safari |
---|---|---|---|---|
Description
The atan() method returns the arctangent of a number as a numeric value between -PI/2 and PI/2 radians.
This method has the form:
Math.atan(x)
Parameter | Description |
---|---|
x | (required) A number |
Examples
The following example shows the basic use of this method.
<script>
document.write(Math.atan(0));
document.write("<br />");
document.write(Math.atan(-1));
document.write("<br />");
document.write(Math.atan(1));
</script>
This produces the following result:
Browser Support
Firefox | IE | Chrome | Opera | Safari |
---|---|---|---|---|
Description
The atan2() method returns the arctangent of the quotient of its arguments. The angle returned is a numeric value between PI and -PI and represents the counterclockwise angle in radians (not degrees) between the positive X axis and the point (x, y).
This method has the form:
Math.atan2(x,y)
Parameter | Description |
---|---|
x | (required) A number |
y | (required) A number |
Examples
The following example shows the basic use of this method.
<script>
document.write(Math.atan2(8,4));
</script>
This produces the following result:
Browser Support
Firefox | IE | Chrome | Opera | Safari |
---|---|---|---|---|
Description
The ceil() method rounds a number UPWARDS to the nearest integer, and returns the result.
This method has the form:
Math.ceil(x)
Parameter | Description |
---|---|
x | (required) A number |
Examples
The following example shows the basic use of this method.
<script>
document.write(Math.ceil(123.45));
</script>
This produces the following result:
Browser Support
Firefox | IE | Chrome | Opera | Safari |
---|---|---|---|---|
Description
The cos() method returns the cosine of a number.
Note: The cos() method returns a numeric value between -1 and 1, which represents the cosine of the angle.
This method has the form:
Math.cos(x)
Parameter | Description |
---|---|
x | (required) A number |
Examples
The following example shows the basic use of this method.
<script>
document.write(Math.cos(0));
document.write("<br />");
document.write(Math.cos(Math.PI));
</script>
This produces the following result:
Browser Support
Firefox | IE | Chrome | Opera | Safari |
---|---|---|---|---|
Description
The E property returns the Euler's number and the base of natural logarithms, approximately 2.718.
This property has the form:
Math.E
Examples
The following example shows the basic use of this property.
<script>
document.write("Euler's number: " + Math.E);
</script>
This produces the following result:
Browser Support
Firefox | IE | Chrome | Opera | Safari |
---|---|---|---|---|
Description
The exp() method returns the value of Ex, where E is Euler's number (approximately 2.7183) and x is the number passed to it.
This method has the form:
Math.exp(x)
Parameter | Description |
---|---|
x | (required) A number |
Examples
The following example shows the basic use of this method.
<script>
document.write(Math.exp(1));
</script>
This produces the following result:
Browser Support
Firefox | IE | Chrome | Opera | Safari |
---|---|---|---|---|
Description
The floor() method rounds a number DOWNWARDS to the nearest integer, and returns the result.
This method has the form:
Math.floor(x)
Parameter | Description |
---|---|
x | (required) A number |
Examples
The following example shows the basic use of this method.
<script>
document.write(Math.floor(123.45));
</script>
This produces the following result:
Browser Support
Firefox | IE | Chrome | Opera | Safari |
---|---|---|---|---|
Description
The LN2 property returns the natural logarithm of 2, approximately 0.693.
This property has the form:
Math.LN2
Examples
The following example shows the basic use of this property.
<script>
document.write("LN2: " + Math.LN2);
</script>
This produces the following result:
Browser Support
Firefox | IE | Chrome | Opera | Safari |
---|---|---|---|---|
Description
The LN10 property returns the natural logarithm of 10, approximately 2.302.
This property has the form:
Math.LN10
Examples
The following example shows the basic use of this property.
<script>
document.write("LN10: " + Math.LN10);
</script>
This produces the following result:
Browser Support
Firefox | IE | Chrome | Opera | Safari |
---|---|---|---|---|
Description
The log() method returns the natural logarithm (base E) of a number.
This method has the form:
Math.log(x)
Parameter | Description |
---|---|
x | (required) A number |
Examples
The following example shows the basic use of this method.
<script>
document.write(Math.log(0));
document.write("<br />");
document.write(Math.log(1));
</script>
This produces the following result:
Browser Support
Firefox | IE | Chrome | Opera | Safari |
---|---|---|---|---|
Description
The LOG2E property returns the base-2 logarithm of E, approximately 1.442.
This property has the form:
Math.LOG2E
Examples
The following example shows the basic use of this property.
<script>
document.write("LOG2E: " + Math.LOG2E);
</script>
This produces the following result:
Browser Support
Firefox | IE | Chrome | Opera | Safari |
---|---|---|---|---|
Description
The LOG10E property returns the base-10 logarithm of E, approximately 0.434.
This property has the form:
Math.LOG10E
Examples
The following example shows the basic use of this property.
<script>
document.write("LOG10E: " + Math.LOG10E);
</script>
This produces the following result:
Browser Support
Firefox | IE | Chrome | Opera | Safari |
---|---|---|---|---|
Description
The max() method returns the number with the highest value.
This method has the form:
Math.max(n1,n2,...,nX)
Parameter | Description |
---|---|
n1,n2,...,nX | (required) One or more numbers. If no arguments are given, the result is -Infinity |
Examples
The following example shows the basic use of this method.
<script>
document.write(Math.max(2,4,-2,6,-1));
</script>
This produces the following result:
Browser Support
Firefox | IE | Chrome | Opera | Safari |
---|---|---|---|---|
Description
The min() method returns the number with the lowest value.
This method has the form:
Math.min(n1,n2,...,nX)
Parameter | Description |
---|---|
n1,n2,...,nX | (required) One or more numbers. If no arguments are given, the result is Infinity |
Examples
The following example shows the basic use of this method.
<script>
document.write(Math.min(2,4,-2,6,-1));
</script>
This produces the following result:
Browser Support
Firefox | IE | Chrome | Opera | Safari |
---|---|---|---|---|