JavaScript Math Object



Navigation Aids -- This Page        Navigation Aids -- This Topic        Navigation Aids -- This Site






Quick Reference for the Math Object



The Math Object Reference Table
Built-In Object
Properties Methods
 
E
LN10
LN2
LOG10E
LOG2E
PI
SQRT1_2
SQRT2
abs( )
acos( )
asin( )
atan( )
atan2( )
ceil( )
cos( )
exp( )
floor( )
log( )
max( )
min( )
pow( )
random( )
round( )
sin( )
sqrt( )
tan( )




Math Object Description

The Math object is an object not a class. The Math object is one of two JavaScript Core built-in objects. The other built-in object is the Global object. The two built-in objects are a subset of the core native classes. However, the built-in objects behave very differently from their cousins, the native objects. Take this link if a comparison of the two types of core objects are of interest.

The Math object defines properties that refer to convenient mathematical constants and functions. You invoked the Math properties using the Math name and the access operator (dot operator) along with the property name. The Math object provides methods and properties that can be used anywhere from within your JavaScript code. In other words, the members of the Math object are always within scope.

Don't forget the with statement! You can save time by avoiding the need to code the object name "Math" in front of the Math's methods and constants.





Math Object Properties


The Pre-defined Constants of the Math Object
Property Value Description
 
Math.E 2.718281828459045 the base of the natural logarithms (Euler's constant)
Math.LN10 2.302585092994046 the natural logarithm of 10
Math.LN2 0.6931471805599453 the natural logarithm of 2
Math.LOG10E 0.4342944819032518 the base-10 logarithm of e (base-10 logarithm of Euler's constant)
Math.LOG2E 1.4426950408889633 the base-2 logarithm of e (base-2 logarithm of Euler's constant)
Math.PI 3.141592653589793 PI
Math.SQRT1_2 0.7071067811865476 the square root of ½ (one-half)
Math.SQRT2 1.4142135623730951 the square root of 2




Math Object Methods


The Pre-defined Methods of the Math Object
Syntax / Arguments Returns / Throws Description / Example
 
Method:
Math.abs( )

Syntax:
Math.abs(num)

Arguments:
num: any Number
Returns:
the absolute value of the argument
The Math.abs( ) method behaves as follows:
  • this method returns an absolute value of a number
  • the argument must be a number or an expression that evaluates to a number
  • automatic type conversion will occur when argument values are not of expected numeric data type

Example:
//both statements will return 1 alert(Math.abs(1)); alert(Math.abs(-1));
Method:
Math.cos( )

Syntax:
Math.cos(num)

Arguments:
num: a angle measured in radians
Returns:
the cosine of the argument
The Math.cos( ) method is a trigonometric function and behaves as follows:
  • this method returns the cosine of the argument
  • the returned value is in radians
  • the argument should be radians and not degrees
  • to convert degrees into radians = (degrees * (Math.PI / 180))
Example: to convert degrees to radians
//to find the radians of 90 degrees var radians = 90 * (Math.PI / 180) alert(radians);// returns 1.5707963267948965 Example: to find cosine
//now find the cosine of our radian value alert(Math.cos(radians)); //returns 6.123031769111886e-17
Method:
Math.sin( )

Syntax:
Math.sin(num)

Arguments:
num: a angle measured in radians
Returns:
the sine of the argument
The Math.sin( ) method is a trigonometric function and behaves as follows:
  • this method returns the sine of the argument
  • the returned value is in radians
  • the argument should be radians and not degrees
  • to convert degrees into radians = (degrees * (Math.PI / 180))

Example: to convert 90 degrees to radians
Example: to find sin of radians (1.5707963267948965)
alert(Math.sin(radians)); //returns 1
Method:
Math.tan( )

Syntax:
Math.tan(num)

Arguments:
num: a angle measured in radians
Returns:
the tangent of the argument
The Math.tan( ) method is a trigonometric function and behaves as follows:
  • this method returns the tangent of the argument
  • the returned value is in radians
  • the argument should be radians and not degrees
  • to convert degrees into radians = (degrees * (Math.PI / 180))

Example: to convert 90 degrees to radians
Example: to find tangent of radians (1.5707963267948965)
alert(Math.tan(radians)); //returns 16331778728383844
Method:
Math.acos( )

Syntax:
Math.acos(num)

Arguments:
num: a number between -1.0 and 1.0
Returns:
a number between 0 and PI
The Math.acos( ) method is a trigonometric function and behaves as follows:
  • the arc cosine is the opposite (inverse) function of the cosine
  • this method returns the arc cosine of the argument
  • the argument number is between -1.0 and 1.0
  • the returned value is in radians; it is a number between 0 and PI
  • the argument should be radians and not degrees
  • to convert degrees into radians = (degrees * (Math.PI / 180))

Example: to convert 90 degrees to radians
Example: to find cosine
Example: to find arc cosine (inverse of cosine)
alert(Math.acos(6.123031769111886e-17)); //arc cosine returns 1.5707963267948965
Method:
Math.asin( )

Syntax:
Math.asin(num)

Arguments:
num: a number between -1.0 and 1.0
Returns:
a number between -PI/2 and PI/2
The Math.asin( ) method is a trigonometric function and behaves as follows:
  • the arc sine is the opposite (inverse) function of the sine
  • this method returns the arc sine of the argument
  • the argument number is between -1.0 and 1.0
  • the returned value is in radians; it is a number between -PI/2 and PI/2
  • the argument should be radians and not degrees
  • to convert degrees into radians = (degrees * (Math.PI / 180))

Example: to convert 90 degrees to radians
Example: to find sine
Example: to find arc sine (inverse of sine)
alert(Math.asin(1)); //arc sine returns 1.5707963267948965
Method:
Math.atan( )

Syntax:
Math.atan(num)

Arguments:
num: any number
Returns:
a number between -PI/2 and PI/2
The Math.atan( ) method is a trigonometric function and behaves as follows:
  • the arc tangent is the opposite (inverse) function of tangent
  • this method returns the arc tangent of the argument
  • the argument number can be any number
  • the returned value is in radians; it is a number between -PI/2 and PI/2
  • the argument should be radians and not degrees
  • to convert degrees into radians = (degrees * (Math.PI / 180))

Example: to convert 90 degrees to radians
Example: to find tangent
Example: to find arc tangent (inverse of tangent)
alert(Math.atan(16331778728383844)); //arc tangent returns 1.5707963267948965
Method:
Math.atan2( )

Syntax:
Math.atan2(num)

Arguments:
y: Y-Coordinate of a point
x: X-Coordinate of a point
Returns:
a number between -PI and PI
The Math.atan2( ) method is a trigonometric function and behaves as follows:
  • this method returns the arc tangent of the argument (ratio y/x)
  • the returned value is in radians; it is a number between -PI and PI
  • the returned value specifies the counterclockwise angle between the X-axis and the point (y,x)
  • the atan2 method requires two arguments, y and x
  • x = the X coordinate of a point
  • y = the Y coordinate of a point
  • y is the first argument specified; x is the second argument
  • to convert the returned value (radians) into degrees = (radians * (180 / Math.PI))

Example:
alert(Math.atan2(1,1)); //atan2 returns 0.7853981633974483 radians //to get answer in degrees alert(Math.atan2(1,1) * (180/Math.PI)); //returns 45 degrees
Method:
Math.round( )

Syntax:
Math.round(num)

Arguments:
num: any number
Returns:
an integer closest to num
The Math.round( ) method will round decimal numbers into integers and behaves as follows:
  • the number returned is an integer closest to the argument
  • this method rounds to nearest whole integer
  • Math.round is the traditional rounding method where .5 is the midpoint and .5 will round up
  • negative numbers round in the same manner
  • the argument can be any number or expression that evaluates to a number

alert(Math.round(4.5));// returns 5 alert(Math.round(4.511));// returns 5 alert(Math.round(4.4999999));// returns 4 alert(Math.round(0.001));// returns 0 alert(Math.round(-1.987));// returns -2
Method:
Math.ceil( )

Syntax:
Math.ceil(num)

Arguments:
num: any number
Returns:
a integer greater than or equal to the argument
The Math.ceil( ) method will round decimal numbers into integers and behaves as follows:
  • the number returned is an integer
  • this method always rounds up to the nearest whole integer
  • Math.ceil is not the same as Math.round
  • negative numbers do not round up to the larger negative number, but they round to the nearest integer toward zero
  • the argument can be any number or expression that evaluates to a number

alert(Math.ceil(1.987));// returns 2 alert(Math.ceil(1.007));// returns 2 alert(Math.ceil(1.0));// returns 1 alert(Math.ceil(0.001));// returns 1 alert(Math.ceil(-1.987));// returns -1
Method:
Math.floor( )

Syntax:
Math.floor(num)

Arguments:
num: any number
Returns:
a integer less than or equal to the argument
The Math.floor( ) method will round decimal numbers into integers and behaves as follows:
  • the number returned is an integer
  • this method always rounds down to the nearest whole integer
  • negative numbers do round down to the larger negative number
  • the argument can be any number or expression that evaluates to a number

alert(Math.floor(1.987));// returns 1 alert(Math.floor(1.007));// returns 1 alert(Math.floor(1.0));// returns 1 alert(Math.floor(0.001));// returns 0 alert(Math.floor(-1.987));// returns -2
Method:
Math.min( )

Syntax:
Math.min(arg,,,,,)

Arguments:
arg,,,,, a set of numbers separated by commas
Returns:
the smallest number in the argument set
The Math.min( ) method behaves as follows:
  • this method determines the smallest number in the argument set
  • the function returns up to 16 significant digits
  • the number returned can be either a decimal or an integer depending on the smallest argument
  • the function returns Infinity when there are no arguments
  • the function returns NaN for NaN argument
  • the argument set can be zero or more values separated by commas
  • prior to ECMAScript v3, this method only accepted two arguments

alert(Math.min(1,9,8,7));// returns 1 alert(Math.min(1,2,3,4,0));// returns 0 alert(Math.min(1,0,-1,2,3,4));// returns -1 alert(Math.min(.001, 0,99));// returns 0.001 alert(Math.min(NaN));// returns NaN alert(Math.min( ));// returns Infinity alert(Math.min(abc));// script error
Method:
Math.max( )

Syntax:
Math.max(arg,,,,,)

Arguments:
arg,,,,, a set of numbers separated by commas
Returns:
the largest number in the argument set
The Math.max( ) method behaves as follows:
  • this method determines the largest number in the argument set
  • the function returns up to 16 significant digits
  • the number returned can be either a decimal or an integer depending on the smallest argument
  • the function returns -Infinity when there are no arguments
  • the function returns NaN for NaN argument
  • the argument set can be zero or more values separated by commas
  • prior to ECMAScript v3, this method only accepted two arguments

alert(Math.max(1,9,8,7));// returns 9 alert(Math.max(1,2,3,4,0));// returns 4 alert(Math.max(1,0,-1,2,3,4));// returns 4 alert(Math.max(.001, 0,99));// returns 99 alert(Math.max(NaN));// returns NaN alert(Math.max( ));// returns -Infinity alert(Math.max(abc));// script error
Method:
Math.pow( )

Syntax:
Math.pow(x,y)

Arguments:
x: the number
y: the power
Returns:
x to the power of y (xy)
The Math.pow( ) method behaves as follows:
  • Math.pow computes x to the power of y
  • this method returns the result of xy
  • the pow method requires two arguments, x and y
  • x = the number to be raised to a power (y)
  • y = the power that x is to be raised to
  • x is the first argument specified; y is the second argument
  • This method can be used to compute the square root
  • Math.pow(2,1/2) is equivalent to Math.sqrt(2)

Example:
alert(Math.pow(1,1));// returns 1 alert(Math.pow(2,2));// returns 4 alert(Math.pow(3,3));// returns 27 alert(Math.pow(4,4));// returns 256 alert(Math.pow(5,5));// returns 3125 alert(Math.pow(5,100));// returns 7.888609052210118e+69 alert(Math.pow(2,-2));// returns .25
Method:
Math.sqrt( )

Syntax:
Math.sqrt(x,y)

Arguments:
x: the number
y: the power
Returns:
x to the power of y (xy)
The Math.sqrt( ) method behaves as follows:
  • Math.sqrt computes the squre root of the number
  • this method returns the square root of the argument
  • the argument must be greater than or equal zero (>=0)
  • A negative number as the argument returns NaN
  • Math.sqrt(2,1/2) is equivalent to Math.sqrt(2)

Example:
alert(Math.sqrt(1));// returns 1 alert(Math.sqrt(2));// returns 1.4142135623730951 alert(Math.sqrt(9));// returns 3 alert(Math.sqrt(-9));// returns NaN
Method:
Math.random( )

Syntax:
Math.random()

Arguments: None
Returns:
a number between 0.0 and 1.0
The Math.random( ) method behaves as follows:
  • Math.random takes no argument
  • this method returns a decimal number up to 16 decimal places
  • the result does not produce an integer, however,
  • x = Math.ceil(Math.random() * 10);
  • will give you a number between 1 and 10

Example:
alert(Math.random()); //returns a random number like 0.4481604157902104
Method:
Math.exp( )

Syntax:
Math.exp(x)

Arguments:
x: the exponent
Returns:
returns ex
The Math.exp( ) method behaves as follows:
  • Math.exp returns ex where x is the exponent and e = Math.E
  • for its argument, Math.exp takes a number that is used as an exponent (x)
  • the exponent is used to raise the value Math.E to the power specified by the exponent
  • Math.E is the the base of the natural logarithms

Example:
alert(Math.exp(1));// returns the value of Math.E alert(Math.exp(2));// returns 7.38905609893065 alert(Math.exp(3));// returns 20.085536923187668
Method:
Math.log( )

Syntax:
Math.log(x)

Arguments:
x: number greater than zero
Returns:
returns the natural logarithm of x
The Math.log( ) method behaves as follows:
  • for its argument, Math.log takes a number greater than zero
  • Math.log(x) computes logex, the natural logarithm of its argument

Example:
alert(Math.log(1));// returns 0 alert(Math.log(2));// returns 0.6931471805599453 alert(Math.log(3));// returns 1.0986122886681097


Top            

Rx4AJAX        About Us | Topic Index | Contact Us | Privacy Policy | 2008 This Site Built By PPThompson