Below is the example of the Math max() method.
- Example:
<script type="text/javascript">document.write("When positive numbers are passed"+" as parameters: "+ Math.max(10, 32, 2));</script>chevron_rightfilter_none - Output:
When positive numbers are passed as parameters: 32
The 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 as ingle parameter that can be used n number of times as mentioned above and described below:
- value: This values sent to math.max() method for finding the largest.
Return vlues: The Math.max() method returns the largest of the given numbers.
Below examples illustrates the Math max() method in JavaScript:
- Example 1:
Input : Math.max(10, 32, 2) Output: 32
- Example 2:
Input : Math.max(-10, -32, -1) Output: -1
- Example 3:
Input : Math.max() Output: -Infinity
- Example 4:
Input : Math.max(10,2,NaN) Output: NaN
More codes for the above method are as follows:
Program 1: When negative numbers are passed as parameters.
<script type="text/javascript"> document.write("Result : " + Math.max(-10, -32, -1)); </script> |
Output:
Result : -1
Program 2: When no parameters are passed.
<script type="text/javascript"> document.write("Result : " + Math.max()); </script> |
Output:
Result : -Infinity
Program : When NaN is passed as a parameter.
<script type="text/javascript"> document.write("Result : " + Math.max(10,2,NaN)); </script> |
Output:
Result : NaN
Supported Browsers:
- Google Chrome
- Internet Explorer
- Firefox
- Opera
- Safari
Recommended Posts:
- PHP Math Functions (is_nan, pow, sqrt, exp, log, log10, log1p, max, min, getrandmax, rand, mt_rand)
- Find the min/max element of an Array using JavaScript
- Max/Min value of an attribute in an array of objects in JavaScript
- How to select Min/Max dates in an array using JavaScript ?
- How to limit a number between a min/max value in JavaScript ?
- JavaScript Math cosh() Method
- JavaScript Math random() Method
- JavaScript Math round( ) Method
- JavaScript Math abs( ) Method
- JavaScript Math sqrt( ) Method
- JavaScript Math floor() Method
- JavaScript Math pow( ) Method
- JavaScript Math trunc( ) Method
- JavaScript Math exp( ) Method
- JavaScript Math log( ) Method
- JavaScript Math cos( ) Method
- JavaScript Math tan( ) Method
- JavaScript Math ceil( ) Method
- JavaScript Math sin( ) Method
- JavaScript Math min( ) Method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


