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 the 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.
  1. <!DOCTYPE html>  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4. </head>  
  5. <body>  
  6.     <script>  
  7.         var number = 20.934567;  
  8.         console.log("Abstract value is:" + Math.round(number));  
  9.     </script>  
  10. </body>  
  11. </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”.
  1. <!DOCTYPE html>  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4. </head>  
  5. <body>  
  6. <script>  
  7.     var number = 20.934567;  
  8.     console.log("Ceil value is :- " + Math.ceil(number));  
  9. </script>  
  10. </body>  
  11. </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.
  1. <!DOCTYPE html>  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4. </head>  
  5. <body>  
  6. <script>  
  7.     var x = 200;  
  8.     var y = 300;  
  9.     var z = 400;  
  10.     console.log("Maximum value is :- " + Math.max(x, y, z));  
  11. </script>  
  12. </body>  
  13. </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:
  1. <!DOCTYPE html>  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4. </head>  
  5. <body>  
  6. <script>  
  7.     var x = 200;  
  8.     var y = 300;  
  9.     var z = 400;  
  10.     console.log("Minimum value is :- " + Math.min(x, y, z));  
  11. </script>  
  12. </body>  
  13. </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.
  1. <!DOCTYPE html>  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4. </head>  
  5. <body>  
  6.     <script>  
  7.         console.log("Random value is :- " + Math.random());  
  8.     </script>  
  9. </body>  
  10. </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.
  1. <!DOCTYPE html>  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4. </head>  
  5. <body>  
  6. <script>  
  7.     console.log("Log of 10 is :- " + Math.log(10));  
  8. </script>  
  9. </body>  
  10. </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.


Similar Articles