Updated April 18, 2023
Introduction to JavaScript max
Javascript max uses the Math.max to find out the maximum number among the list of the numeric values that are supplied to the function as its parameters. This method helps to retrieve the maximum numeric value. However, this does not work with the arrays. If you want to find out the maximum valued element in the array, then you will have to use additional functionality such as the array.reduce method or apply() method along with Math.max method to find out the maximum valued element. In this article, we will see the syntax, general usage, and working of the Math.max method and demonstrate the usage of the Math.max method with the help of examples for a list of numbers and array.
Syntax:
Math.max([numericValue [, numberValue2 [, ...]]])
Syntax of JavaScript max function is defined:
Math, present in JavaScript, is the object that has multiple functions and properties defined in it to help carry out various mathematical operations. Max is one such function present in the Math object, and it is the static method. Hence, whenever you want to use the max() function, then you will need to mention the Math word before the max() method separated by a dot(.) Such as Math.max();
[numericValue [, numberValue2 [, …]]] – This are the optional numeric values that you can pass as the parameter to the max() method to find out the largest value among the passed parameter values. When none of the numeric value is passed, then the result of the max() function is -Infinity that is the initial value of the comparator. Nearly all other numeric values have a value greater than -Infinity; hence, we do not mention any numeric value as a parameter to be compared and get maximum value, the max() function return -Infinity value. If you pass a value that cannot be converted to a number as a parameter to themax()
function, then the output of the max()
function will be NaN, which stands for Not a Number.
Examples of JavaScript max
Given below are the examples of JavaScript max:
Example #1
Consider a simple example where we try to get the maximum value among different positive and negative numbers, as shown below.
Code:
<!DOCTYPE html>
<html>
<body>
<h2>Javascript Math.max() method demonstration</h2>
<p id="demo1"></p>
<p id="demo2"></p>
<p id="demo3"></p>
<script>
document.getElementById("demo1").innerHTML = "Largest value among is 5, 6, 8, 12, 10 : "+ Math.max(5, 6, 8, 12, 10);
document.getElementById("demo2").innerHTML = "Largest value among is 5, -6, 8, -12, 10 : "+ Math.max(5, -6, 8, -12, 10);
document.getElementById("demo3").innerHTML = "Largest value among is -6, -8, -7, -9, -45 : "+ Math.max(-6, -8, -7, -9, -45);
</script>
</body>
</html>
Output:
We know that the negative numbers decrease in value as and when their absolute numerical value increases, which means -45 is less than -9 and so on. Hence, the output of the largest value among all the negative numbers supplied as parameter -6, -8, -7, -9, -45 is -6.
Example #2
Consider another example, where we try to display the value returned by Math.max() function when none of the parameters is supplied and when string value is passed and when array value is passed as a parameter to the function as shown in the below example.
Code:
<!DOCTYPE html>
<html>
<body>
<h2>Javascript Math.max() method demonstration</h2>
<p id="demo1"></p>
<p id="demo2"></p>
<p id="demo3"></p>
<script>
document.getElementById("demo1").innerHTML = "Default value returned by Math.max() function when no parameter is specified: "+ Math.max();
document.getElementById("demo2").innerHTML = "Largest value among string specified parameter 5, -6, 8, 'EDUCBA', 10 is: "+ Math.max(5, -6, 8, 'EDUCBA', 10);
document.getElementById("demo3").innerHTML = "Largest value among array [-6, -8, -7, -9, -45] is: "+ Math.max([-6, -8, -7, -9, -45]);
</script>
</body>
</html>
Output:
As seen earlier, for non-convertible to numeric value parameters such as strings and arrays, the output of the Math.max() function is NaN. The output for no parameter specified Math.max() method is -Infinity.
Example #3
Let us now look, how we can get the largest value among the array elements using the following example.
Code:
<!DOCTYPE html>
<html>
<body>
<h2>Javascript Math.max() method for array using Array.reduce() method demonstration</h2>
<p id="demo"></p>
<script>
var array = [3445, 3346, 4358, 3412, 4310];
var maximumValue = array.reduce(function(temp1, temp2) {
return Math.max(temp1, temp2);
});
document.getElementById("demo").innerHTML = "Largest value among array [3445, 3346, 4358, 3412, 4310] is "+ maximumValue;
</script>
</body>
</html>
Output:
Example #4
Let us see one more method to find out the maximum value in the array using another technique that uses apply() function.
Code:
<!DOCTYPE html>
<html>
<body>
<h2>Javascript Math.max() method for array using apply() method demonstration</h2>
<p id="demo"></p>
<script>
var array = [3445, 3346, 4358, 3412, 4310];
function getMaximumValue(suppliedArray) {
return Math.max.apply(null, suppliedArray);
}
document.getElementById("demo").innerHTML = "Largest value among array [3445, 3346, 4358, 3412, 4310] is "+ getMaximumValue(array);
</script>
</body>
</html>
Output:
Example #5
We can use one more method called the spread operator method that can be used to get the maximum value of an array.
Code:
<!DOCTYPE html>
<html>
<body>
<h2>Javascript Math.max() method for array using spread operator method demonstration</h2>
<p id="demo"></p>
<script>
var array = [3445, 3346, 4358, 3412, 4310];
var maxValue = Math.max(...array);
document.getElementById("demo").innerHTML = "Largest value among array [3445, 3346, 4358, 3412, 4310] is "+ maxValue;
</script>
</body>
</html>
Output:
Conclusion
We can find out the largest value among the specified numeric values as parameters to the max() function by using the Math.max() function in javascript. However, we need to be careful while using it with other types of values and arrays. When you want to find out the largest value in an array, you will have to use some additional functionality, code, and logic along with Math.max() method to do so. Such as we can use apply() method or the Array.reduce() method for finding max value in an array.
Recommended Articles
This is a guide to JavaScript max. Here we discuss the introduction to JavaScript max along with appropriate examples, respectively. You may also have a look at the following articles to learn more –