JavaScript Kit > JavaScript Reference > Here
Math Object
Last updated: June 20th, 2004
The Math object allows you to perform common math related tasks. Here are a few example uses:
var mygrade=Math.round(8.6) //returns 9
var mynum=Math.pow(2, 3) //returns 8
Related Tutorials
- The Math object
- Number rounding in JavaScript
- Generating a random number in JavaScript
- Generating weighed random numbers
Properties
Properties | Description |
---|---|
E | The constant of E, the base of natural logarithms. |
LN2 | The natural logarithm of 2. |
LN10 | The natural logarithm of 10. |
LOG2E | Base 2 logarithm of E. |
LOG10E | Base 10 logarithm of E. |
PI | Returns PI. |
SQRT1_2 | Square root of 1/2. |
SQRT2 | Square root of 2. |
Methods
Methods | Description |
---|---|
abs(x) | Returns absolute value of x. |
acos(x) | Returns arc cosine of x in radians. |
asin(x) | Returns arc sine of x in radians. |
atan(x) | Returns arc tan of x in radians. |
atan2(y, x) | Counterclockwise angle between x axis and point (x,y). |
ceil(x) | Returns the smallest integer greater than or equal to x. (round up). |
cos(x) | Returns cosine of x, where x is in radians. |
exp(x) | Returns ex |
floor(x) | Returns the largest integer less than or equal to x. (round down) |
log(x) | Returns the natural logarithm (base E) of x. |
max(a, b) | Returns the larger of a and b. |
min(a, b) | Returns the lesser of a and b. |
pow(x, y) | Returns Xy |
random() | Returns a pseudorandom number between 0 and 1. Example(s). |
round(x) | Rounds x up or down to the nearest integer. It rounds .5 up. Example(s). |
sin(x) | Returns the Sin of x, where x is in radians. |
sqrt(x) | Returns the square root of x. |
tan(x) | Returns the Tan of x, where x is in radians. |
Examples
random()
Math.random() //returns random number between 0 and 1, such as 0.634343434...
Math.floor(Math.random()*11) //returns random integer between 0 and 10
round(x)
Math.round(25.9) //returns 26
Math.round(25.2) //returns 25
Math.round(-2.58) //returns -3
var original=28.453
//round "original" to two decimals:
Math.round(original*100)/100 //returns 28.45
Reference List
- JavaScript Operators
- JavaScript Statements
- Global functions
- JavaScript Events
- Escape Sequences
- Reserved Words
Right column