JavaScript Math max() Method (original) (raw)
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:
- **value: These values are sent to **math.max() method for finding the largest.
**Return values:
- If one or more numbers are passed as arguments, Math.max() will return the largest of them.
- Math.max() will return -Infinity if no arguments are provided.
- If any argument cannot be converted to a number, the method will return NaN (Not-a-Number).
**Example 1: Basic Usage of Math.max()
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));
`
Output
When positive numbers are passed as parameters: 32
**Example 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());
`
**Example 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 Browsers
- Google Chrome 1
- Firefox 1
- Opera 3
- Safari 1