Math.max()

Returns the largest of the numbers passed to it, or -Infinity when given none.

Signature
Math.max(value1, value2, /* …, */ valueN)
  • Added in JavaScript ES1

Parameters

ParameterTypeDescription
$value1, …, valueNoptionalnumberZero or more numbers among which the largest is selected. Non-numbers are coerced first.

Return value

numberThe largest of the given numbers. Returns NaN if any argument is or converts to NaN, and -Infinity if called with no arguments.

Errors

Nothing on its own, but spreading a very large array into it can exceed the engine's argument limit and throw a RangeError.

Math.max() is variadic: it takes any number of arguments and returns the largest. It does not take an array, so the usual call is Math.max(...arr).

When to reach for it

Two everyday jobs: finding the peak of a small data set, and clamping a lower bound. Math.max(0, value) is the idiomatic "never go below zero", and Math.min(hi, Math.max(lo, value)) is the standard clamp.

What trips people up

Three things, in order of how often they bite:

  • An empty array returns -Infinity. Math.max(...[]) is -Infinity, because that is the identity for maximum. Guard the empty case before displaying the result.
  • One NaN poisons everything. Math.max(1, NaN, 3) is NaN — and since arguments are coerced, a single undefined or non-numeric string does the same. Filter first if the data is untrusted.
  • Spreading a huge array can throw. Arguments go on the call stack, so around 100,000+ elements (the exact limit is engine-specific) Math.max(...arr) raises a RangeError. Use reduce for large arrays.

Note that Math.max.length is 2, which only reflects the spec's declared arity and says nothing about how many arguments it accepts.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Clamping a value to a minimum with Math.max(0, n)
  • Finding the peak value in a small array of readings
  • Sizing a container to the widest of several measured items
  • Keeping a countdown from going negative

Frequently asked questions

How do I find the maximum value of an array in JavaScript?

Use Math.max(...array) to spread the elements into arguments. For arrays over roughly 100,000 elements use array.reduce((a, b) => b > a ? b : a, -Infinity) instead, since spread can exceed the engine's argument limit.

Why does Math.max() return -Infinity?

Because you called it with no arguments, which usually means you spread an empty array. -Infinity is the identity value for maximum, so any real number compared against it wins.

Why does Math.max() return NaN?

Because at least one argument is NaN or coerces to NaN — an undefined, a non-numeric string, or an object. Filter with Number.isFinite before calling if the input is not trusted.

More examples

Large arrays: use reduce, not spread

Spread turns every element into a separate argument, and engines cap how many arguments a call may have. reduce keeps a single accumulator and works at any size. Some engines raise RangeError here while others cope, which is exactly why spread is unreliable for large inputs.

Example · javascript
const big = Array.from({ length: 500000 }, (_, i) => (i * 37) % 99991);

// Safe for any length
const max = big.reduce((a, b) => (b > a ? b : a), -Infinity);
console.log(max);

try {
  console.log(Math.max(...big));
} catch (err) {
  console.log(err.constructor.name); // often RangeError
}

Guarding against NaN and empty input

Filtering to finite numbers first stops a single bad value from turning the whole result into NaN, and returning null for empty input avoids handing -Infinity to a UI. The last line shows the unguarded behaviour for comparison.

Example · javascript
function safeMax(values) {
  const nums = values.map(Number).filter(Number.isFinite);
  return nums.length ? Math.max(...nums) : null;
}

console.log(safeMax([3, "7", 5]));          // 7
console.log(safeMax([3, "oops", 5]));       // 5
console.log(safeMax([]));                   // null
console.log(Math.max(...[3, "oops", 5]));   // NaN

Discussion

  • Be the first to comment on this lesson.