### Introduction:

This tutorial will deal with Math object of JavaScript which will be use for performing mathematical tasks or calculations.

JavaScript math object contains various built-in methods and mathematical constants for performing mathematical calculations in scripts. The Math object of JavaScript allows you to perform certain calculations by using method functions of the Math object. It provides a few constants such as pi, log values and exponential values. For this we have to define a variable and set its value through a property or function of the Math Object.

If you want to use a math property which returns a constant value, then you have to write like this, for getting value of pi in a simple variable you would have to use.

[ var value=math.property ] [var pi_value=Math.pi ] //This variable return value of pi 3.14 [var my_money=Math.sqrt(x);]

Most of the member functions have one or more parameters, which is what the "x" is for. You can replace x with a number or variable. For instance, if you want the square root of a number, you can call the square root member function of the Math object with the number as the parameter.

[ var my_money=Math.sqrt(16); ]

There are few rules and regulation for using math object; you have to remember while using math object. The "M" in Math is always capitalized. The word Math is followed by the dot operator (.), and then the property or member function you want to use. Here we will deal with some most common method and properties.

**Listing 1:** Script to finding round off of a number

<html> <head> <title>JavaScript Math Object</title> </head> <body> <script type="text/javascript"> document.write(Math.round(16.12345) + "<br />") document.write(Math.round(25.0)+ "<br />") document.write(Math.round(36.111) + "<br />") document.write(Math.round(49.992222233) + "<br />") </script> </body> </html>

** Figure 1: ** Above figure is output of round function used in Math object ,which round off the passed no.

Lets discuss another example uses the max() method of the Math object which returns the largest of the given numbers given in arguments of the max method.

**Listing 2:** Script to find max number between two number

<html> <head> <title>JavaScript Math Object</title> </head> <body> <script type="text/javascript"> document.write(Math.max(3000,2999, 4999) + "<br />") document.write(Math.max(-2000,5000) + "<br />") document.write(Math.max(-2,-7, 0, +0, 0.1) + "<br />") document.write(Math.max(22,-7) + "<br />") </script> </body> </html>

**Figure 2:** Above figure is output of math function

Another function will demonstrate sqrt() function. Which giver output of square root. Following listed script will compile the idea of sqrt() function.

** Listing 3:** Script to calculate squire root of given number

<html> <body> <script type="text/javascript"> document.write(Math.sqrt(16) + "<br />") document.write(Math.sqrt(25) + "<br />") document.write(Math.sqrt(36) + "<br />") document.write(Math.sqrt(49) + "<br />") document.write(Math.sqrt(64) + "<br />") </script> </body> </html>

**Figure 3:** Above figure is output of sqrt() function.

There is another example, Assume you have a point with the (x,y) coordinates of (14,18), you could calculate the angle in radians between that point and the positive X axis as follows. For this purpose Math object provide a function atan2(X, ).following example shows the idea of atan2(X,Y). Below calculating angle in both radian and degree. To convert radian into degree, multiply radian with 180*7/22

** Listing 4:** Script to calculate angle

<html> <head> <title>JavaScript Math Object</title> </head> <body> <script type="text/javascript"> document.write(Math.atan2(8,8)*180/Math.PI + "<br />") document.write(Math.atan2(32,16)*180/Math.PI+ "<br />") document.write(Math.atan2(64,128)*180/Math.PI + "<br />") document.write(Math.atan2(128,256) + "<br />") document.write(Math.atan2(256,512) + "<br />") </script> </body> </html>

** Figure 4: ** Above figure is output of atain2 () function in which atan2() method returns the arctangent of the quotient of its arguments, as a numeric value between PI and -PI radians. The number returned represents the counter clockwise angle in radians (not degrees) between the positive X axis and the point (x, y).

JavaScript provide the eight mathematical constant that can be handle by JavaScript Math objects these are: ln2 , ln10, log2e, log10e, e, pi, sqrt2, sqrt1_2 . And provide various mathematical function which is handle by math object of JavaScript in various way.

### Conclusion:

This tutorial covered the basic concepts related to math object of JavaScript. I would suggest that go through the examples given with this article and explore more functions and properties of Math object. Please comment at mrbool site, if you have any questions.