How to Find the Largest Number in an Array in JavaScript

Find the largest number in an array in JavaScript
Find the largest number in an array in JavaScript Math.max()

1. Find the Largest Number in an Array in Javascript

The shortest way to find the largest number in an array in JavaScript is to use the built-in static method Math.max(). Math.max() returns the largest of the numbers which it takes as input parameters. The arguments are given separated with commas, for example, Math.max(1, 2, 3, 4) returns 4. We can’t pass an array as an argument, so we’ll use the spread operator (…) to split the array into separated elements and pass them to the method Math.max(…arr).

const arr = [-45, 31, 51, 61, -2];

const max = Math.max(...arr);
console.log(max); // returns 61

2. Find the Largest Number in an Array Using for Loop

2.1. Steps | Algorithm

  1. Create a new variable where you’ll keep the largest number. (let max)
  2. Set an initial value to the newly created variable. Choose one of the following options for the initial value:
    • let max = arr[0] —> the first element of the array
    • let max = -Infinity
    • let max = Number.MIN_VALUE
    • let max =  Number.NEGATIVE_INFINITY
  3. Loop through the array and compare each element with the current max value (the max variable). 
    • If the current item of the array is larger than the current max value, set a new value for the max variable: max = value of the current element (the new value)
    •  If the current element isn’t larger or it equals the max value – do nothing
const arr = [-45, 31, 51, 61, -2];
// ---> set an initial value to max
let max = arr[0]; 
// or let max = Number.MIN_VALUE;
// or let max = Number.NEGATIVE_INFINITY;
// or let max = -Infinity;

for( let i = 0; i < arr.length; i++) {
  if(arr[i] > max) { // if the current item is larger than current max
    max = arr[i];    // it will assign the value of arr[i] to the max variable
  }
}

console.log(max); // returns 61

2.2. Each Iteration Explained

The below table shows what happens on each iteration of the for loop in order to find the largest number:

iteration/iconditioncomparisonresultcurrent max
0arr[0] > max -45 > -45 false
1arr[1] > max31 > -45truenew max —> max = 31
2arr[2] > max51 > 31truenew max —> max = 51
3arr[3] > max61 > 51truenew max —> max = 61
4arr[4] > max-2 > 61false
Iterating through the array to find the largest number

3. Find the Largest Number in an Array Using foreach

The steps are almost the same as in the previous example: 2.1. Steps | Algorithm. But in this case, we will use the built-in iterative foreach method. We’ll use it instead of for loop.

const arr = [-45, 31, 51, 61, -2];

// ---> set an initial value to max
let max = arr[0];
// or let max = Number.MIN_VALUE;
// or let max = Number.NEGATIVE_INFINITY;
// or let max = -Infinity;

//loop through each element
arr.forEach(item => {
  if(item > max) { // if the current item is larger than current max 
    max = item;  // it will assign the value of arr[i] to the max variable
  }
});

console.log(max); // returns 61

In this case, the item represents the current element of the array, so instead of arr[index] in the column “condition” in the above table, we will have item (Look – 2.2. Each iteration explained ). The process is analogical.

4. Find the Largest Number in an Array Using .sort()

The sort method takes a function as an argument. The function has two parameters, so for each step, the sort method will compare these two parameters.

4.1. Sort the Array in Ascending Order

  1. Sort the array in ascending order using the built-in sort() method in JavaScript.
  2. Get the last element using the length property of the array.
const arr = [-45, 31, 51, 61, -2];

//arr[[arr.length - 1] - returns the last value of the array
const max = arr.sort((a, b) => a - b)[arr.length - 1];
console.log(max); // returns 61

The below table shows how the sort method compares the arguments of the passed function (a, b). In this case, the initial value of a is the second element of the array; b is the first element of the array. If you debug the above example, you’ll get the same result. As you can see, the last number is the largest, so we extract it with arr[arr.length – 1].

Na – bresultshiftorder
031 – (-45)76 > 0 —> positive number sort 31(a) after -45(b)[ -45, 31, 51, 61, -2]
151 – 3120 > 0 —> positive number sort 51(a) after 31(b)[-45, 31, 51, 61, -2]
261 – 5110 > 0 —> positive number sort 61(a) after 51(b)[-45, 31, 51, 61, -2]
3(-2) – 61-63 < 0 —> negative numbersort -2(a) before 61(b)[-45, 31, 51, -2, 61]
4(-2) – 51-53 < 0 —> negative numbersort -2(a) before 51(b)[-45, 31, -2, 51, 61]
5(-2) – 31-33 < 0 —> negative numbersort -2(a) before 31(b)[-45, -2, 31, 51, 61]
6(-2) – (-45)43 > 0 —> positive numbersort -2(a) after -45(b)[-45, -2, 31, 51, 61]
Find the largest number in an array using .sort() -sort method debugging

4.2. Sort the Array in Descending Order

  1. Sort the array in descending order using the built-in sort() method in JavaScript.
  2. Get the first element of the array(at index = 0).
const arr = [-45, 31, 51, 61, -2];

const max = arr.sort((a, b) => b - a)[0];
console.log(max); // returns 61

5. Find the Largest Number in an Array Using .reduce()

You can find the largest number using the reduce() method and Math.max(). The reduce() method takes a callback function as a parameter and executes it on each item of the array. The result of it would be passed on to the preceding element. Reduce() takes a second parameter – initial value. In our case, it will be -Infinity.

const arr = [-45, 31, 51, 61, -2];

const max = arr.reduce((a, b) => Math.max(a, b), -Infinity);
console.log(max);

Let’s debug the reduce method. Each iteration is shown in the below table.

NMath.max(a, b)result – max
0-Infinity, -45-45
1-45, 3131
231, 5151
351, 6161
461, -261
Find the largest number in an array using .reduce() and Math.max()

6. Find the Largest Number in an Array Using for of

const arr = [-45, 31, 51, 61, -2];
let max = arr[0];

for (const element of arr) {
 if(element > max) {
  max = element;
 }
}

console.log(max); //61

Examples 2, 3, and 6 are the same but we’ve used different loops.

Conclusion

As you can see the first example is the simplest way to find the largest number in an array in JavaScript. Furthermore, we use the ES6 spread operator which reduces our code.

Similar Posts