Math Functions in JavaScript

JavaScript FAQ | JavaScript Numbers FAQ  

Question: What mathematical functions are supported in JavaScript?

Answer: JavaScript supports many arithmetic operations as well as the following math functions (or, more precisely, methods of the Math object):

Math.abs(a)     // the absolute value of a
Math.acos(a)    // arc cosine of a
Math.asin(a)    // arc sine of a
Math.atan(a)    // arc tangent of a
Math.atan2(a,b) // arc tangent of a/b
Math.ceil(a)    // integer closest to a and not less than a
Math.cos(a)     // cosine of a
Math.exp(a)     // exponent of a (Math.E to the power a)
Math.floor(a)   // integer closest to a, not greater than a
Math.log(a)     // log of a base e
Math.max(a,b)   // the maximum of a and b
Math.min(a,b)   // the minimum of a and b
Math.pow(a,b)   // a to the power b
Math.random()   // pseudorandom number 0 to 1 (see examples)
Math.round(a)   // integer closest to a (see rounding examples)
Math.sin(a)     // sine of a
Math.sqrt(a)    // square root of a
Math.tan(a)     // tangent of a
Note that trigonometric functions assume that the argument is in radians, not degrees!

See also:
How do I plot a function graph in JavaScript?
Number literals in JavaScript
Mathematical constants in JavaScript
Octals and hexadecimals in JavaScript
Can I display mathematical symbols as part of JavaScript output?
Can I display Greek letters on my page as part of JavaScript output?
Doing Math with JavaScript

Copyright © 1999-2012, JavaScripter.net.