Skip to content
Related Articles

Related Articles

JavaScript Math max() Method

Improve Article
Save Article
  • Last Updated : 29 Nov, 2022
Improve Article
Save Article

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: 

  • value: These values are sent to math.max() method for finding the largest.

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

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

Example 1: 

javascript




<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. 

javascript




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


Output: 

Result : -1

Example 3: When no parameters are passed. 

javascript




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


Output: 

Result : -Infinity

Example 4: When NaN is passed as a parameter. 

javascript




<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: 

  • Google Chrome 1 and above
  • Internet Explorer 3 and above
  • Firefox 1 and above
  • Opera 3 and above
  • Safari 1 and above

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.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!