Math Functions in JavaScript

Introduction

In this article we will learn various math functions in JavaScript. Math functions are needed to perform mathematical operations in applications. The Math object allows you to perform mathematical tasks on numbers.

  1. Round(): How to use round().
  2. Ceil(): Ceil() function is used to get higher rounded value of any real number.
  3. Max(): How to use max() to return the number with the highest value of two specified numbers.
  4. Min(): How to use min() to return the number with the lowest value of two specified numbers.
  5. Random(): How to use random() to return a random number between 0 and 1.
Round() function to get absolute value

The Round() function discards the fractional part from a number. In this example we are using the round() function on a real number. Have a look at the following example.

<!DOCTYPE html>

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

<head>

</head>

<body>

    <script>

        var number = 20.934567;

        console.log("Abstract value is:" + Math.round(number));

    </script>

</body>

</html> 

round function
Ceil() Function

The Ceil() function gets the higher rounded value of a real number. In this example we are applying the ceil() function to a real variable called “number”.

<!DOCTYPE html>

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

<head>

</head>

<body>

<script>

    var number = 20.934567;

    console.log("Ceil value is :- " + Math.ceil(number));

</script>

</body>

</html> 

Ceil Function
Max Function

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

<!DOCTYPE html>

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

<head>

</head>

<body>

<script>

    var x = 200;

    var y = 300;

    var z = 400;

    console.log("Maximum value is :- " + Math.max(x, y, z));

</script>

</body>

</html>

Max Function

Min Function

The Min() function is used to get a minimum value among a set of values. We can supply a direct value or specify a variable as an argument of the min() function. Try to understand the following example:

<!DOCTYPE html>

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

<head>

</head>

<body>

<script>

    var x = 200;

    var y = 300;

    var z = 400;

    console.log("Minimum value is :- " + Math.min(x, y, z));

</script>

</body>

</html> 

Min Function
Random() Function

The Random() function is very useful to get a random number in JavaScript. It generates a random number between 0 and 1. This function might be useful for various JavaScript games.

<!DOCTYPE html>

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

<head>

</head>

<body>

    <script>

        console.log("Random value is :- " + Math.random());

    </script>

</body>

</html>

Random Function
Log Function

The log() function will return the logarithmic value of a number. In this example we are supplying 10 as the argument of the 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> 

Log function
Summary

In this article we learned a few important math functions of JavaScript. In future articles we will learn a few more interesting concepts in JavaScript.

Up Next
    Ebook Download
    View all
    Learn
    View all