Saturday, September 21, 2024
Google search engine
HomeLanguagesJavascriptJavaScript Math max() Method

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

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

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

javascript




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


Output: 

Result : -1

Example 3: When no parameters are passed. 

javascript




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


Output: 

Result : -Infinity

Example 4: When NaN is passed as a parameter. 

javascript




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: 

  • Google Chrome 1 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.

RELATED ARTICLES

Most Popular

Recent Comments