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.

Suppose any input parameter is not a number and cannot be transformed into one. In that case, the Math.max() function returns NaN instead of the largest of the zero or more values that were provided as input parameters.

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 (Math is not a constructor).

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, value3, …, 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 and that can be used to compare any number of integers.

The variable value1 shows the largest between a combination of positive and negative integers. The variable value2 shows the largest among negative integers. The variable value3 shows the largest number between 0 and -1.

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

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 and min variables store the first element of the array. The for-loop traverses through every element in the array and stores the maximum element. If the second element is greater than the next element, the largest will be stored in the max variable. The max variable is then printed on the user’s screen.

<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], min = 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>

In this tutorial, what we have learned about is how to find the largest of zero or more numbers in JavaScript. The first method discusses the usage of the Math.max() method. The second method uses an array to find out the largest among the array elements in JavaScript. Both these methods are simple and exact for the reader to understand this concept.

Updated on: 26-Aug-2022

203 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements