JavaScript Math.max() and Math.min() to Find the Largest and Smallest Values

In JavaScript, Math.max(a, b, c) returns the largest number and Math.min(a, b, c) returns the smallest. Spread a small numeric array with Math.max(...values). For a large array, find both values in one pass.

Find the largest and smallest of several numbers

console.log(Math.max(2, 5, -3)); // 5
console.log(Math.min(2, 5, -3)); // -3
console.log(Math.max(-8, -2)); // -2

const values = [2, 5, -3];
console.log(Math.max(...values)); // 5
console.log(Math.min(...values)); // -3
console.log(Math.max(...[])); // -Infinity
console.log(Math.min(...[])); // Infinity
console.log(Math.max(2, NaN)); // NaN

With no arguments, Math.max() returns -Infinity and Math.min() returns Infinity. Both return NaN if any argument becomes NaN during conversion. ECMAScript · Math.max() ECMAScript · Math.min()

Choose an approach for arrays of different sizes

Spreading an array passes one argument per element. Very large arrays can exceed the runtime’s argument limit. The loop below compares only two numbers at a time. It returns null for an empty array or an element that is not a finite number.

function extrema(values) {
  if (values.length === 0) {
    return null;
  }
  let min = Infinity;
  let max = -Infinity;
  for (const value of values) {
    if (!Number.isFinite(value)) {
      return null;
    }
    min = Math.min(min, value);
    max = Math.max(max, value);
  }
  return { min, max };
}

console.log(extrema([2, 5, -3])); // { min: -3, max: 5 }
console.log(extrema([]));         // null

For text input, reject empty fields before converting with Number(). Number('') is 0, so a misplaced comma can otherwise introduce a zero. To restrict the accepted notation, see JavaScript number conversion and decimal validation.

Compare a list of numbers in a complete example

Save the code as math-max-min.html in UTF-8 and open it in a browser. Enter comma-separated values and select Compare. The default 2, 5, -3 has a maximum of 5 and a minimum of -3.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Compare maximum and minimum</title>
</head>
<body>
<label for="numbers">Numbers, separated by commas</label>
<input id="numbers" value="2, 5, -3">
<button id="compare" type="button">Compare</button>
<pre id="result" aria-live="polite"></pre>
<script>
const input = document.querySelector('#numbers');
const result = document.querySelector('#result');
const invalid = "Enter finite numbers separated by commas.";

function extrema(values) {
  if (values.length === 0) {
    return null;
  }
  let min = Infinity;
  let max = -Infinity;
  for (const value of values) {
    if (!Number.isFinite(value)) {
      return null;
    }
    min = Math.min(min, value);
    max = Math.max(max, value);
  }
  return { min, max };
}

document.querySelector('#compare').addEventListener('click', () => {
  const parts = input.value.split(',').map(part => part.trim());
  if (parts.some(part => part === '')) {
    result.textContent = invalid;
    return;
  }

  const limits = extrema(parts.map(Number));
  if (limits === null) {
    result.textContent = invalid;
    return;
  }
  result.textContent = `Maximum: ${limits.max}\nMinimum: ${limits.min}`;
});
</script>
</body>
</html>

-8, -2 gives -2 and -8, and a single 4 gives 4 for both. A trailing comma or Infinity displays an input message.