Open In App

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:

Return values:

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

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






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: When negative numbers are passed as parameters. 




console.log("Result : " + Math.max(-10, -32, -1));

Output
Result : -1

Example 3: When no parameters are passed. 




console.log("Result : " + Math.max());

Output
Result : -Infinity

Example 4: When NaN is passed as a parameter. 




console.log("Result : " + Math.max(10,2,NaN));

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 :