The Wayback Machine - https://web.archive.org/web/20250113215233/https://www.geeksforgeeks.org/javascript-math-max-method/
Open In App

JavaScript Math max() Method

Last Updated : 30 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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));

Output
Result : -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());

Output
Result : -Infinity

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));

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
  • Firefox 1
  • Opera 3
  • Safari 1

JavaScript Math.max() Method – FAQs

What does the Math.max() method do in JavaScript?

The Math.max() method returns the largest of zero or more numbers.

What happens if you pass non-numeric values to Math.max()?

If you pass non-numeric values, Math.max() will attempt to convert them to numbers. Non-convertible values will result in NaN (Not-a-Number) as the result.

How does Math.max() handle different data types like negative numbers?

Math.max() handles negative numbers alongside positive numbers and zero. It returns the largest numeric value provided.

Is Math.max() inclusive of all provided numbers?

Yes, Math.max() considers all numbers passed to it and returns the largest one. If no arguments are provided, it returns -Infinity.

What is the most common use of the Math.max() method?

Most Common Use Cases:

  • Determining the highest score in a set of game scores.
  • Finding the largest dimension (length, width, height) among objects.
  • Implementing sorting algorithms or custom comparisons.

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.

Become an expert in solving problems with DSA JavaScript—the course designed to teach you Data Structures and Algorithms using JavaScript. Over the next 90 days, dive deep into key DSA concepts, learn to optimize your code, and gain hands-on experience solving challenging problems. Whether you're a beginner or looking to refine your JavaScript skills, this course will provide the knowledge you need to succeed.
Take on the Three 90 Challenge today! Complete 90% of the course within 90 days, and you’ll earn a 90% refund. This challenge is your chance to stay motivated, improve your skills, and get rewarded for your progress. Don’t wait—start your journey to DSA mastery with JavaScript today!


Next Article

Similar Reads

three90RightbarBannerImg