JavaScript Math max() Method

The JavaScript Math max() Method is used to return the largest of zero or more numbers. The result is “-Infinity” if no arguments are passed and the result is NaN if at least one of the arguments cannot be converted to a number.

The max() is a static method of Math, therefore, it is always used as Math.max(), rather than as a method of a Math object created.

Syntax:

Math.max(value1, value2, ...)

Parameters: This method accepts a single parameter that can be used n number of times as mentioned above and described below: 

Return values: The Math.max() method returns the largest of the given numbers.

Below is an example of the Math max() method.  

Example 1: 






<script type="text/javascript">
    console.log("When  positive numbers are passed"+
                   " as parameters: " + Math.max(10, 32, 2));
</script>

Output: 

When  positive numbers are passed as parameters: 32

Example 2 : When negative numbers are passed as parameters. 




<script type="text/javascript">
         console.log("Result : " + Math.max(-10, -32, -1));
</script>

Output: 

Result : -1

Example 3: When no parameters are passed. 




<script type="text/javascript">
         console.log("Result : " + Math.max());
</script>

Output: 

Result : -Infinity

Example 4: When NaN is passed as a parameter. 




<script type="text/javascript">
         console.log("Result : " + Math.max(10,2,NaN));
</script>

Output: 

Result : NaN

We have a complete list of Javascript Math Objects methods, to check those please go through this Javascript Math Object Complete reference article.

Supported Browsers: 

We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.


Article Tags :