HOWTO · JavaScript

How to Find Max/Min Value in JavaScript Array

We can find the minimum and maximum value of an array using the Math.min() and Math.max() function in JavaScript.

On this page

Find the maximum or minimum number in a JavaScript array with Math.max() or Math.min(). Use apply() to pass the array values as arguments, or use a loop when you need explicit control over the calculation or are working with a very large array.

Find the Maximum Value with Math.max()

Math.max() compares its arguments and returns the greatest number. Since it accepts separate arguments rather than an array, apply() passes each array element as an argument. The first parameter is the this value; null is suitable here because Math.max() does not use one.

Run this example:

const numbers = [1, 5, 6, 2, 3];
console.log(Math.max.apply(null, numbers));

The result is the greatest value in numbers. Keep the input numeric: if an element is NaN, the result is also NaN. This form is convenient for ordinary arrays, and the same pattern works with Math.min() when you need the smallest value.

Find the Minimum Value with Math.min()

Math.min() uses the same argument pattern and returns the smallest number. For an array named numbers, call Math.min.apply(null, numbers). The apply() call supplies the array elements individually, so you do not need to write each value as a separate argument.

For example, this complete program applies the pattern to a small array:

const numbers = [1, 5, 6, 2, 3];
console.log(Math.min.apply(null, numbers));

The returned value is the least element. Use Math.max() and Math.min() for numeric data; neither method sorts or changes the original array. If values may not be numbers, convert or validate them before calculating an extreme so that comparisons have the meaning your application expects.

Use apply() or a Loop as Alternatives

A loop avoids passing every element as a function argument. Initialize the running value from the first element, then compare each later value. The following functions return undefined when their input array has no elements, and otherwise return the corresponding extreme:

function maximum(numbers) {
  if (numbers.length === 0) return undefined;
  let result = numbers[0];
  for (let i = 1; i < numbers.length; i++) {
    if (numbers[i] > result) result = numbers[i];
  }
  return result;
}

function minimum(numbers) {
  if (numbers.length === 0) return undefined;
  let result = numbers[0];
  for (let i = 1; i < numbers.length; i++) {
    if (numbers[i] < result) result = numbers[i];
  }
  return result;
}

const numbers = [1, 5, 6, 2, 3];
console.log(maximum(numbers));
console.log(minimum(numbers));

Starting from an actual element keeps the result valid for arrays containing only negative numbers or only positive numbers. The local result variable also prevents the calculation from creating or changing a global variable. These loops assume the elements are numbers; validate mixed or missing data before comparing it.

Handle Empty Arrays and Large Arrays

Calling Math.max() with no arguments returns -Infinity, while calling Math.min() with no arguments returns Infinity. Applying either method to an empty array passes no arguments, so those are also the results of Math.max.apply(null, []) and Math.min.apply(null, []). Decide whether those values make sense for your application or check numbers.length first and return a value such as undefined instead, as the loop functions above do.

apply() turns array elements into function arguments. JavaScript engines impose practical limits on the number of arguments a call can receive, and the limit can depend on the environment. A sufficiently large array can therefore make the apply() call fail. Use a loop for large inputs; it examines values one at a time and does not create a call with one argument per array element.