Last Updated : 30 Sep, 2024
The Math.max() method in JavaScript returns the largest value from a set of zero or more numbers. This method is a static method of the Math object, meaning it is always called Math.max() and not as a method of an instance of the Math object.
Syntax:Math.max(value1, value2, ...);Parameters:
Here's a simple example of how the Math.max() method works:
javascript
console.log("When positive numbers are passed" +
" as parameters: " + Math.max(10, 32, 2));
When positive numbers are passed as parameters: 32Example 2: Handling Negative Numbers with Math.max()
Math.max() also works with negative numbers, returning the largest (least negative) value:
javascript
console.log("Result : " + Math.max(-10, -32, -1));
Example 3: What Happens When No Parameters are Passed
If no arguments are passed to Math.max(), the method will return -Infinity:
javascript
console.log("Result : " + Math.max());
Result : -InfinityExample 4: Handling NaN with Math.max()
If any argument passed to Math.max() cannot be converted to a number, the method will return NaN:
javascript
console.log("Result : " + Math.max(10,2,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 BrowsersRetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4