Open In App

JavaScript Math max() Method

Last Updated : 08 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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:

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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads