 ×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

# How to Work with JavaScript Math Object

This article will cover the JavaScript Math Objects. I will demonstrate the functionality by giving some examples of round off, max number and angle calculation

How to Work with JavaScript Math Object

### 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); ]
```
This gives back the square root of 4.

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>
<title>JavaScript Math Object</title>
<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>
<title>JavaScript Math Object</title>
<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>
<title>JavaScript Math Object</title>
<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. I am Software Developer from India. I hold B.Tech. in Computer Science & Engineering. I am having 2+ years experience of developing Web applications and Websites using HTML, JavaScript, CSS, C#.NET etc..

What did you think of this post?
Services

Related Posts

[Close] See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US\$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US\$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US\$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!