Did you find this useful?
Socialize it today.


JavaScript Math Object

Sunday, April 22, 2012

The JavaScript Math object is built-in to JavaScript and includes mathematical constants and functions. The Math object is not a constructor. Unlike other objects, it is not necessary to create the Math object before using it.


String Object Syntax

var a = Math.property;
document.write(Math.method); 


Math Object Properties

JavaScript provides eight mathematical properties (constants) that can be accessed from the Math object.


PropertyDescription
EThe mathematical constant e. This is Euler's number; the base of natural logarithms.
LN2The natural logarithm of 2.
LN10The natural logarithm of 10.
LOG2EBase 2 logarithm of E.
LOG10EBase 10 logarithm of E.
PIReturns the ratio of the circumference of a circle to its diameter; PI.
SQRT1_2The square root of 1/2.
SQRT2The square root of 2.


Here is an example of how to use the PI property:


document.write(Math.PI);


Math Object Methods

The following table lists the methods of the Math object.


MethodDescription
abs(x)Returns the absolute value of x.
acos(x)Returns the arccosine of x.
asin(x)Returns the arcsine of x.
atan(x)Returns the arctangent of a number.
atan2(y,x)Returns the angle from the X axis to a point represented by the supplied y and x coordinates.
ceil(x)Returns x, rounded upwards to the nearest integer.
cos(x)Returns the cosine of x.
exp(x)Returns e raised to a power.
floor(x)Returns x, rounded downwards to the nearest integer.
log(x)Returns the natural logarithm 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.
sqrt(x)Returns the square root of x.
tan(x)Returns the tangent of an angle.


Here is an example of how to use the Math max method:


document.write(Math.max(5,7,9,10,2,1));


Always keep in mind that JavaScript is case sensitive.

Please help us spread the word by socializing it today!

email contact us

Did you find something wrong with the information on this page? Please take a moment to report it to us so that we can continue to improve the quality of the information on this site. Click here to report an issue with this page.



Recommended Books & Training Resources

Murachs JavaScript and DOM Scripting HTML CSS and JavaScript Editor Professional JavaScript for Web Developers