Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to get the largest of zero or more numbers in JavaScript?
In this tutorial, we will look at how to get the largest of zero or more numbers in JavaScript.
The maximum of multiple numbers can be found using different techniques. The first technique we will introduce is the Math.max() method. The second technique will be the for-loop method.
Following are the methods to get the largest of zero or more numbers in JavaScript ?
Using the Math.max() Method
We can use the Math.max() method to find the largest of many numbers. It returns the largest value from the given multiple values. The input parameters should be numbers or can be transformed into numbers.
If any input parameter is not a number and cannot be transformed into one, the Math.max() function returns NaN. You always use max() as Math.max() rather than as a method of a Math object you constructed since max() is a static method of Math.
In the absence of any arguments, the outcome is -Infinity. The outcome is NaN if at least one of the arguments cannot be transformed into a number.
Syntax
Following is the syntax for the Math.max() method ?
Math.max(value0, value1, value2, ..., valueN);
The Math.max() method takes multiple values as parameters and returns the largest element among them.
Example
In this example, we use the Math.max() method to find out which one value is the largest number among multiple values. This is a built-in JavaScript method that can be used to compare any number of integers.
<html>
<body>
<h3>Get largest number using the <i>Math.max()</i> method.</h3>
<p id="result1"></p>
<p id="result2"></p>
<p id="result3"></p>
</body>
<script>
let result1 = document.getElementById("result1");
let result2 = document.getElementById("result2");
let result3 = document.getElementById("result3");
let value1 = Math.max(244, 220, -70, 10);
result1.innerHTML = "Math.max(244, 220, -70, 10) = " + value1;
let value2 = Math.max(-98, -21, -93);
result2.innerHTML = "Math.max(-98, -21, -93) = " + value2;
let value3 = Math.max(0, -1);
result3.innerHTML = "Math.max(0, -1) = " + value3;
</script>
</html>
Math.max(244, 220, -70, 10) = 244 Math.max(-98, -21, -93) = -21 Math.max(0, -1) = 0
Using the for-loop
In this method, we can make use of an array. The array will take a set of numbers as the input. We make use of a for-loop to traverse through the array of elements and store the largest number from the array in a variable.
Syntax
Following is the syntax for the for-loop method ?
for (let i = 0; i < arr.length; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
The for-loop is used to traverse the elements in the array while comparing all the elements one by one and storing the maximum element in the max variable.
Example
In this example, we initially create an array of elements to find the largest of all numbers. This array contains a combination of positive and negative integers.
The max variable stores the first element of the array. The for-loop traverses through every element in the array and stores the maximum element. If any element is greater than the current max, it will be stored in the max variable.
<html>
<body>
<h3>Get largest number using the <i>For loop</i> method</h3>
<p id="root"></p>
</body>
<script>
let root = document.getElementById("root");
const array = [-111, 444, -909, 22, 37];
let max = array[0];
for (let i = 0; i < array.length; i++) {
// If the element is greater than the max value, replace max
if (array[i] > max) {
max = array[i];
}
}
root.innerHTML = "max([-111, 444, -909, 22, 37]) = " + max;
</script>
</html>
max([-111, 444, -909, 22, 37]) = 444
Using Math.max() with Arrays
You can also use the spread operator (...) to apply Math.max() directly to an array:
<html>
<body>
<h3>Using Math.max() with spread operator</h3>
<p id="result"></p>
</body>
<script>
const numbers = [15, 42, 7, 89, 3];
const largest = Math.max(...numbers);
document.getElementById("result").innerHTML =
"Math.max(...[15, 42, 7, 89, 3]) = " + largest;
</script>
</html>
Math.max(...[15, 42, 7, 89, 3]) = 89
Edge Cases
Here are some important edge cases to consider:
<html>
<body>
<h3>Edge cases with Math.max()</h3>
<p id="edge1"></p>
<p id="edge2"></p>
<p id="edge3"></p>
</body>
<script>
document.getElementById("edge1").innerHTML =
"Math.max() with no arguments = " + Math.max();
document.getElementById("edge2").innerHTML =
'Math.max("hello", 5) = ' + Math.max("hello", 5);
document.getElementById("edge3").innerHTML =
"Math.max(null, undefined, 10) = " + Math.max(null, undefined, 10);
</script>
</html>
Math.max() with no arguments = -Infinity
Math.max("hello", 5) = NaN
Math.max(null, undefined, 10) = NaN
Conclusion
Math.max() is the most efficient method for finding the largest number among multiple values. For arrays, combine it with the spread operator, or use a for-loop for more control over the comparison logic.
