comparison
Math.max
Returns the largest of zero or more numbers passed as separate arguments. With no arguments returns -Infinity.
Math.max(...values) Parameters & edge cases
| Case | Behavior |
|---|---|
| ...values | Any number of numeric arguments (0 or more) |
| no args | Math.max() returns -Infinity (the identity for the max operation) |
| NaN input | Any NaN argument makes the result NaN |
| arrays | Spread with Math.max(...arr); huge arrays can hit call-stack limits |
Examples
console.log(Math.max(1, 2, 3)); // 3 Straightforward variadic max.
console.log(Math.max()); // -Infinity No args returns -Infinity so that max(a, Math.max()) === a.
console.log(Math.max(...[1, 2, 3])); // 3 Spread an array — required because Math.max([1,2,3]) returns NaN.
console.log(Math.max(1, NaN)); // NaN NaN is contagious; filter with .filter(Number.isFinite) first.
Gotcha
For arrays over ~10⁵ elements, Math.max(...arr) can throw a RangeError. Use `arr.reduce((a, b) => a > b ? a : b)` for very large arrays.
Related
Math.min
Returns the smallest of zero or more numbers passed as separate arguments. With no arguments returns +Infinity.
Math.abs
Returns the absolute value of a number — its distance from zero without regard to sign. Useful for differences, magnitudes, and tolerance checks.
Math.hypot
Returns the square root of the sum of squares of its arguments — the Euclidean norm. Introduced in ES2015 and safer than manual sqrt(a*a + b*b) against overflow.