Home > DeveloperSection > Articles > JavaScript-Math Object

JavaScript-Math Object


JavaScript JavaScript 
Ratings:
0 Comment(s)
 2738  View(s)
Rate this:

JavaScript-Math Object

Introduction:

In this article I am going to explain you about Math object which provides us several mathematical functionalities. The Math object allows us to perform common mathematical tasks. It includes several Mathematical constants and methods. Unlike other global objects Math is not a constructor all methods and properties of Method are static and all properties and methods of math can be called by using Math as an object without creating it.

Syntax for using Math Methods is:

var roundof = Math.round(0.6);

This function will round of the value of 0.6 to 1.

Math Methods:

Methods

 

Description

 

 

 

abs ()

Returns the absolute value of a number.

acos()

It returns the arccosine of a number.

asin()

It returns the arcsine of a number.

atan()

It returns the arctangent of a number.

ceil()

It returns the smallest integer greater than or equal to a number.

floor()

It returns the largest integer less than or equal to a number.

log()

It returns the Natural logarithm of a number.

max()

It returns the largest number of zero or more number.

min()

It returns the smallest of zero or more number.

round()

It returns the value of a number rounded to the nearest integer.

sin()

It returns the sine of a number.

sqrt()

It returns the square root of a number.

tan()

It returns the tangent of a number.

toSource()

It returns the string “Math”.

pow()

It returns the base to the exponent power i.e. base exponent.

Examples depicting the use of the above methods:

abs():

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

    <script type="text/javascript">

        function findAbsValue() {

           var absVal = Math.abs(-2);

            var absNull = Math.abs(null);

            document.write("Absolute value of -2 is : " + absVal + "<br />");

            document.write("Absolute value is of null is : " + absNull + "<br />");

        }

    </script>

</head>

<body onload="findAbsValue()">

</body>

</html>

 

Output:

JavaScript-Math Object

 

acos():

 

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title></title>

  <script type="text/javascript">

     function findAcos() {

         var value = Math.acos(-1);

         document.write("acos value of -1 is: " + value + "<br />");

         var value = Math.acos(null);

  document.write("acos value of Null is: " + value + "<br />");

    }

</script>

</head>

<body onload="findAcos()">

</body>

</html>

 

Output:

 

JavaScript-Math Object

ceil():

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

    <title>Java Script Ceil Method</title>

    <script type="text/javascript">

        function getCeil() {

            var firstval = Math.ceil(55.95);

            document.write("Value is: " + firstval + "<br/>");

            firstval = Math.ceil(-55.95);

            document.write("Value is: " + firstval + "<br/>");

        }

    </script>

</head>

<body onload="getCeil()">

</body>

</html>

 

  Output:

JavaScript-Math Object

 

 

floor:

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

    <title>Java Script floor Method</title>

    <script type="text/javascript">

        function getFloor() {

            var firstval = Math.floor(55.95);

            document.write("Value is: " + firstval + "<br/>");

            firstval = Math.floor(-55.95);

            document.write("Value is: " + firstval + "<br/>");

        }

    </script>

</head>

<body onload="getFloor()">

</body>

</html>

 

   Output :

JavaScript-Math Object

 

round():

 

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

    <script type="text/javascript">

        function mathFunction() {

           var roundof = Math.round(0.6);

           document.write("After round of the number is : " + roundof + "<br />");

        }

    </script>

</head>

<body onload="mathFunction()">

</body>

</html>

 

Output:

JavaScript-Math Object

 

 

max():

        

          <html xmlns="http://www.w3.org/1999/xhtml">

     <head>

     <title>Java Script to get the max value</title>

     <script type="text/javascript">

        function getMax() {

            var firstval = Math.max(100, 43, 66, -25);

            document.write("Value is: " + firstval + "<br/>");

        }

     </script>

     </head>

     <body onload="getMax()">

     </body>

     </html>

   

     Output:

 

JavaScript-Math Object

 

 

min():

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

    <title>Java Script to get the max value</title>

    <script type="text/javascript">

        function getMin() {

            var firstval = Math.min(100, 43, 66, -25);

            document.write("Minimum Value is: " + firstval + "<br/>");

        }

    </script>

</head>

<body onload="getMin()">

</body>

</html>

 

Output:

JavaScript-Math Object

Conclusion:

Through this article we came to know about what math function is and how to use its various methods.


Don't want to miss updates? Please click the below button!

Follow MindStick