Math functions in JavaScript- Part 1

Sourav.Kayal
Posted by in JavaScript category on for Beginner level | Points: 250 | Views : 1607 red flag

In this article we will learn various math function in JavaScript

Math functions in JavaScript- Part 1

In this article we will learn various math functions in JavaScript. Math functions are needed to perform mathematical operation in application. Let’s learn one by one of them.

round() function to get absolute value

round() function discard the fraction part from a number. In this example we are using round() function on a real number. Have a look on below example.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
    <script>
        var number = 10.234567;
        console.log("Abstract value is:" + Math.round(number));
    </script>
</body>
</html>


Ceil() function

Ceil() function is used to get higher rounded value of any real number. In this example we are applying ceil() function to a real variable called “number”.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
    <script>
        var number = 10.234567;
        console.log("Ceil value is :- " + Math.ceil (number));
    </script>
</body>
</html>


The ceil() function is returning the next rounded value which is 11 in this article.

 floor() function

It is nothing but opposite of ceil() function. The floor function returns the immediate lower round figure value. Try to understand below example.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
    <script>
        var number = 10.234567;
        console.log("Floor value is :- " + Math.floor (number));
    </script>
</body>
</html>


max function

max() function can return the maximum value from a set of value. In this example we are passing three values as argument of max() function.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
    <script>
        var a = 100;
        var b = 200;
        var c = 300;
        console.log("Maximum value is :- " + Math.max(a,b,c));
    </script>
</body>
</html>


log function

log() function will return the logarithmic value of a number. In this example we are supplying 10 as argument of log function.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
    <script>
        console.log("Log of 10 is :- " + Math.log(10));
    </script>
</body>
</html>

Here is log value of 10.


PI property of math object

In this example we will learn PI property of Math object. We know that PI is nothing but a constant in mathematics and we are printing PI value of math object.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
    <script>
        console.log("Value of PI is :- " + Math.PI);
    </script>
</body>
</html>


Sqrt() function

Sqrt()  function returns square root value of given number. We are passing 3 as a argument of sqrt() function.

 <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
    <script>
        console.log("Square root of 3 is :- " + Math.sqrt(3));
    </script>
</body>
</html>


Conclusion:-

In this article we have learned few important math functions of JavaScript.

 

Page copy protected against web site content infringement by Copyscape

About the Author

Sourav.Kayal
Full Name: Sourav Kayal
Member Level: Silver
Member Status: Member,MVP
Member Since: 6/20/2013 2:09:01 AM
Country: India
Read my blog here http://ctrlcvprogrammer.blogspot.in/
http://www.dotnetfunda.com
I am .NET developer working for HelixDNA Technologies,Bangalore in healthcare domain. Like to learn new technology and programming language. Currently working in ASP.NET ,C# and other microsoft technologies.

Login to vote for this post.

Comments or Responses

Posted by: Sheonarayan on: 11/25/2013 | Points: 25
I was not aware about ceil , floor and log functions of Math object in JavaScript.

Worth reading your JavaScript series of article.

Keep it up!

Login to post response

Comment using Facebook(Author doesn't get notification)