JavaScript

JavaScript Math.max() method | Explained

JavaScript provides numerous mathematical methods such as floor(), sqrt(), max(), random(), etc. All these methods are used to attain different objectives, such as the Math.random() method is used to get a random value between 0 and 1, the Math.sqrt() method is used to find the square root of the user-specified value, and so on. If we talk about the Math.max() method, it is used to find the maximum value among different values.

This post will explain the below-listed perceptions regarding the Math.max() method:

So, let’s start!

What is Math.max() in JavaScript

It is a mathematical method that takes multiple values as arguments and returns the maximum value. The Math.max() method is a static method of the Math object; hence it is invoked using the Math object.

Basic Syntax

The code block given below will provide the basic syntax of JavaScript’s Math.max() method:

Math.max([num1, num2, ... num_n]);

Here, the num1, num2, etc., are the n number of parameters/arguments that a Math.max() can take. The Math.max() method will find the greatest number among these values provided as parameters.

What does Math.max() Method Return

In JavaScript, the Math.max() method can take multiple numbers, compare the provided values, and return the greatest value among them. The Math.max() method will return:

  • NaN(not a number) if the given values are non-numeric, and
  • -Infinity if we didn’t specify any parameter.

How to Use Math.max() Method in JavaScript

As of now we are done with the theoretical part of the Math.max() method. Now, let’s move one step further and practically implement all these theoretical concepts.

Example1: Let’s consider the below-given code to understand the working of the Math.max() method in JavaScript:

<script>

console.log("Maximum Number: " + Math.max(14, 72));

console.log("Maximum Number: " + Math.max(-14, -72));

console.log("Maximum Number: " + Math.max(72, 12, 60));

console.log("Maximum Number: " + Math.max(-12, -5, 0, -18));

</script>

In the above code snippet, we utilized the console.log() method to print the maximum value on the browser’s console:

The output authenticates the working of the Math.max() method as it gave us accurate results.

Example2: In this example, we will see how the Math.max() method will behave if we didn’t provide it any parameters, or if we provide it a non-numeric value:

<script>

console.log("Maximum Number: " + Math.max());

console.log("Maximum Number: " + Math.max(5, 12, 'a'));

</script>

The above code block will produce the following output:

This is how Math.max() method works in JavaScript.

Example3: Let’s consider the below code snippet to understand how to find the maximum value among the array elements using the Math.max() method:

<script>

var findMax = [115, 45, 72, 67, 1, 80, -72];

console.log("Maximum Number: " + Math.max(...findMax));

</script>

The above code will generate the below given output:

The Math.max() method returned “115” as the maximum value which authenticates the working of Math.max() method.

Conclusion

In JavaScript, the Math.max() method can take multiple numbers, compare the provided values, and return the greatest value among them. The Math.max() method returns NaN(not a number) if the given values are non-numeric, and it returns -Infinity if we didn’t specify any parameter. This write-up explained what Math.max() is, what it returns, and how to use it in JavaScript.

About the author

Anees Asghar

I am a self-motivated IT professional having more than one year of industry experience in technical writing. I am passionate about writing on the topics related to web development.