Math functions in JavaScript: Part -2

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

In this article we will learn few important math functions in javaScript

Math functions in JavaScript: Part -2

This is the next part of “Math functions in JavaScript” article. In our previous article we have learned various Math function in JavaScript, in this article we will understand few more of them. 


random() function

random() function is very useful to get random number in JavaScript. It generates random number from 0 to 1. This function might useful for various JavaScript game.

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


min() function

min() function is used to get minimum value among a set of value. We can supply direct value or specify variable as argument of min() function. Try to understand below example

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
    <script>
        var c = 30;
        console.log("Minimum value is :- " + Math.min(10,20,c));
    </script>
</body>
</html>

Here is sample example


pow() function

pow() function is useful to get power of some number. In this example we are calculating 10 to the power  of 2. The first argument is base and second argument is index.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
    <script>
        console.log("10 to the power 2 is :- " + Math.pow(10,2));
    </script>
</body>
</html>

Here is output.


sin() ,cos() and tan() function

In this example we calculate sin, cos and tan value of certain number. Have a look on below example

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

Here is sample output.


abs() function

This function is useful when we want to get absolute value of a number. In this example we are passing a real number as argument of abs() function.

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

In this article we have seen few more math functions in JavaScript.  Hope you have understood the concept.

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
Precise and precious.

Thanks

Login to post response

Comment using Facebook(Author doesn't get notification)