Math Object in JavaScript
JavaScript provides a builtin math object to make mathematical calculations easy. Several mathematical functions and constants are available with Math Object in JavaScript. There are predefined methods to calculate random(), sqrt(), min(),max(), sin(), cos(), floor(),ceil() etc. available in Math Object.
Method |
Description |
Math.random() |
returns a value between 0(included) and 1(excluded) |
Math.round() |
returns the value of x rounded to its nearest integer |
Math.pow(x,y) |
returns the value calculated by applying x to the power of y |
Math.sqrt() |
returns the square root of a number |
Math.abs() |
returns the absolute value(positive) |
Math.ceil() |
returns the rounded up nearest number |
Math.floor() |
returns the rounded down nearest number |
Math.sin() |
return the sine value, the input is the value in radians (* to convert into angles use formula angle*Math.PI/180)) |
Math.cos() |
return the cosine value, the input is the value in radians (* to convert into angles use formula angle*Math.PI/180)) |
Math.min() |
returns the minimum from the argument list |
Math.max() |
returns the maximum from the arguments list |
Constants
Math Object also defines 8 constant values given below,Constant |
Description |
Math.E |
Euler's number |
Math.PI |
PI |
Math.SQRT2 |
The square root of 2 |
Math.SQRT1_2 |
The square root of 1/2 |
Math.LN2 |
Natural Log of 2 |
Math.LN10 |
Natural Log of 10 |
Math.LOG2E |
Base 2 logarithm of E |
Math.LOG10E |
Base 10 logarithm of E |
Example