How to Find the min/max element of an Array using JavaScript

In this post, we will see how to get the min/max elements from an array in JavaScript. The minimum and maximum elements in an array can be found using below methods:

Methods to find min/max from array

  • Using a loop
  • Using reduce method
  • Using built in methods Math.min and Math.max

Method 1 – Using a loop

In this approach, you use a loop to iterate through each element of the unsorted array and keep track of the minimum and maximum values found.

function findMinMax(arr) {
    if (arr.length === 0) {
        return [null, null]; // Return [null, null] for both min and max if the array is empty
    }
    let minVal = maxVal = arr[0]; // Initialize min and max with the first element of the array
    for (let i = 1; i < arr.length; i++) {
        if (arr[i] < minVal) {
            minVal = arr[i]; // Update min if a smaller number is found
        } else if (arr[i] > maxVal) {
            maxVal = arr[i]; // Update max if a larger number is found
        }
    }
   return [minVal, maxVal];
}
const myArray = [3, 1, 9, 4, 5, 7];
const [minNum, maxNum] = findMinMax(myArray);
document.write("Smallest number:", minNum); 
document.write("Largest number:", maxNum); 

Output of the above will be –

Smallest number:1 Largest number:9

In the above example, first, we initialize minVal and maxVal to keep track of min and max values and set them to the first element of the array. Now start iterating through the array from the second element.

For each element in the array, we checked if the number is smaller than minVal, if yes assign it to minVal. If the number is larger than maxVal, we assign it to maxVal.

After iterating all the elements of the array, we will have min and max values in minVal and maxVal.


Method 2 – Using reduce method

We can also make use of the reduce method in JavaScript to find the min/max element from the array.

const numbers = [5, 2, 9, 1, 5, 6];
// Initialize the accumulator with an object containing initial min and max values.
const minMax = numbers.reduce((acc, current) => {
  if (current < acc.min) {
    acc.min = current;
  }
  if (current > acc.max) {
    acc.max = current;
  }
  return acc;
}, { min: numbers[0], max: numbers[0] });
document.write("Minimum:", minMax.min);
document.write("Maximum:", minMax.max);

Output of the above will be –

Minimum:1 Maximum:9

In this example, we have acc object having both min and max initialized to the first element of the array. Then iterate through the array, If the current element is smaller than acc.min, we will set it to acc.min.

In the same way, if the current element is larger than acc.max we will set it to acc.max. At the end we will return acc which will get stored in minMax variable.


Method 3 – Using Math.min and Math.max

In this approach, we will use JavaScript’s built-in functions, Math.min() and Math.max(), along with the spread operator (...), to find the smallest and largest values in the array.

const myArray = [3, 1, 9, 4, 5, 7];
const minNum = Math.min(...myArray);
const maxNum = Math.max(...myArray);
document.write("Smallest number:", minNum);
document.write("Largest number:", maxNum);

Output of the above will be –

Smallest number:1 Largest number:9

Math.min function is used to find the min element from the passed arguments and Math.max function can be used to find the max element from the passed arguments.

In this example, we spread the array and passed the array elements to Math.min and Math.max functions.