JS Math

JS has some good collection of Math functions to handle various requirements. These functions we can inside our script based on the logic required. JavaScript consists of Math object that contains the functions and properties of mathematical operations. We can perform the actions like round figure the number to its nearest integer, evaluate the trigonometric angles using cos(), sin() and tan() functions. Math object also provides the functions like log(), sqrt(), random(), max(), min() etc. Following are the definitions for different types of JS Math Functions:

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.floor(a)   // integer closest to and 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 in the range 0 to 1
Math.round(a)   // integer closest to a
Math.sin(a)     // sine of a
Math.sqrt(a)    // square root of a
Math.tan(a)     // tangent of a

Note: Trigonometric functions assume that the argument is in radians, not degrees.

JS Math object also provides some properties along with functions. JavaScript Math object properties return the constant values for Math constants such as PI or E (i.e. Euler’s constant). Math object properties return pre-calculated values also e.g.: natural Log value of 2 or 10. Following are the JS Math object properties:

Math.E:  returns the value of Euler’s constant. (2.718281828459045)
Math.LN10:  returns the natural log of 10. (2.302585092994046)
Math.LN2:  returns the natural log of 2. (0.6931471805599453)
Math.LOG10E:  returns the base-10 log of E. (0.4342944819032518)
Math.LOG2E:  returns the base-2 log of E. (1.4426950408889633)
Math.PI:  returns the constant value of PI. (22/7 = 3.141592653589793)
Math.SQRT1_2:  returns the value of square root of ½. (0.7071067811865476)
Math.SQRT2:  returns the square root of 2. (1.4142135623730951)

Above JavaScript Math properties return the predefined values for the constants like PI and E. Other properties like LOG10E, LN10, SQRT2 provides the pre- calculated values that reduces the code length by eliminating the logical code for deriving the values each time when they are required. We can directly use these JavaScript Math object properties.

The following JavaScript code shows the example of the Math object.

Example: Illustration of Math Object

document.write(Math.round(0.60) + "<br />");
document.write(Math.round(0.50) + "<br />");
document.write(Math.round(0.49) + "<br />");
document.write(Math.round(-4.40) + "<br />");
document.write(Math.round(-4.60) + "<br />");

document.write(Math.random() + "<br />");

document.write(Math.max(-3,-5) + "<br />");
document.write(Math.max(7.25,7.30) + "<br />");

document.write(Math.min(5,7) + "<br />");
document.write(Math.min(-3,5) + "<br />");