Math class in JavaScript¶
Properties¶
Some properties of the Math object:
| Property | Description |
|---|---|
| E | Euler’s constant |
| LN2 | Natural log of 2 [ln(2)] |
| LN10 | ln(10) |
| LOG2E | log2(e) |
| LOG10E | log10(e) |
| PI | Pi (3.14….) |
Methods¶
Some methods:
| Method | Description |
|---|---|
| abs(x) | absolute value |
| acos(x) | arccos |
| asin(x) | arcsin |
| atan(x) | |
| atan2(y, x) | arctangent of the quotient of its arguments |
| ceil(x) | rounds up to the nearest integer |
| cos(x) | |
| sin(x) | |
| exp(x) | |
| floor(x) | rounds down to the nearest integer |
| log(x) | ln(x) |
| max(x,y,…) | |
| min(x,y,…) | |
| sqrt(x) | |
| tan(x) | |
| random() | random number between 0 and 1 (1 not included) [0, 1[ |
Example: generate a random integer number between 1 and 10
Math.ceil(Math.random() * 10);