Math.max()
Math.max(value1, value2, ...values) The Math.max() function returns the largest of the numbers passed as arguments. If no arguments are provided, it returns -Infinity. If any argument cannot be converted to a number, it returns NaN.
Syntax
Math.max()
Math.max(value1)
Math.max(value1, value2)
Math.max(value1, value2, /* …, */ valueN)
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
| value1, …, valueN | Number | — | Zero or more numbers among which the largest will be selected and returned. |
Return Value
The largest of the given numbers. Returns -Infinity if no arguments are provided. Returns NaN if any argument is not a number or cannot be converted to one.
Examples
Basic usage
Math.max(1, 3, 2);
// 3
Math.max(-1, -3, -2);
// -1
Math.max(1, Infinity);
// Infinity
Calling Math.max() with no arguments returns -Infinity. This is the identity element for the maximum operation — when you take the max of -Infinity and any real number, the result is always the real number. This property makes Math.max() safe to use as a reduce initial value.
No arguments
Math.max();
// -Infinity
Math.max() coerces each argument to a number before comparing. When a string contains a valid numeric value, it is converted automatically. However, if any single argument cannot be converted, the entire result becomes NaN. This means one bad value poisons the whole call, so validate untrusted input beforehand.
With non-numeric values
Math.max(1, "2", 3);
// 3 — string "2" is coerced to number
Math.max(1, "hello", 3);
// NaN — "hello" cannot be converted to a number
To find the maximum in an array, use the spread operator to pass each element as a separate argument. This is the most concise form for small to medium arrays and reads clearly. For very large arrays, however, the spread operator can exceed the engine’s argument count limit, typically around 65,536 arguments.
Finding max in an array with spread
const numbers = [3, 1, 4, 1, 5, 9, 2, 6];
const max = Math.max(...numbers);
console.log(max);
// 9
When the array is large enough that spreading would risk a stack overflow, use Array.prototype.reduce() with Math.max as the reducer. reduce() processes elements one at a time without hitting argument count limits, and starting with -Infinity ensures any value in the array wins the first comparison.
Finding max in a large array with apply
const bigArray = Array.from({ length: 10000 }, (_, i) => i);
// Use reduce for very large arrays to avoid stack overflow
const max = bigArray.reduce((a, b) => Math.max(a, b), -Infinity);
console.log(max);
// 9999
Math.max() pairs naturally with Math.min() for clamping. The expression Math.max(min, Math.min(max, value)) constrains a value to a range by first capping it at the upper bound with Math.min(), then ensuring it does not fall below the lower bound with Math.max(). The nesting order matters — swap them and you get a different result.
Common Patterns
Clamping a value to an upper bound
function atMost(value, ceiling) {
return Math.min(value, ceiling); // use Math.min to cap
}
// Or cap from both sides:
function clamp(value, min, max) {
return Math.max(min, Math.min(max, value));
}
console.log(clamp(150, 0, 100));
// 100
console.log(clamp(-10, 0, 100));
// 0
console.log(clamp(50, 0, 100));
// 50
To find the highest value of a specific property across an array of objects, map the property first and then spread the resulting numeric array into Math.max(). This two-step pattern — extract, then compare — keeps the code readable even when the objects have many fields.
Getting the highest value from an object array
const products = [
{ name: "Widget", price: 9.99 },
{ name: "Gadget", price: 24.99 },
{ name: "Doohickey", price: 4.99 },
];
const highestPrice = Math.max(...products.map(p => p.price));
console.log(highestPrice);
// 24.99
For tracking a running maximum through a stream of values, update an accumulator with Math.max(accumulator, nextValue) on each iteration. Starting the accumulator at -Infinity ensures the first real value always becomes the new maximum, which avoids special-casing the first iteration.
Tracking a running maximum
const scores = [72, 88, 91, 65, 95, 80];
let runningMax = -Infinity;
for (const score of scores) {
runningMax = Math.max(runningMax, score);
console.log(`After ${score}: max so far = ${runningMax}`);
}
// After 72: max so far = 72
// After 88: max so far = 88
// After 91: max so far = 91
// After 65: max so far = 91
// After 95: max so far = 95
// After 80: max so far = 95
Math.max() vs reduce()
For finding the maximum value in an array, Math.max(...array) is the most concise option but has a practical limit: the spread operator passes each array element as a separate function argument, and JavaScript engines have a maximum argument count (typically around 65,536). For arrays larger than this threshold, use array.reduce((a, b) => Math.max(a, b), -Infinity) instead, which processes elements sequentially without hitting the argument limit.
Coercion behaviour
Math.max() coerces non-numeric arguments using the abstract ToNumber operation. Strings containing valid numbers ("3", "1.5") are converted; strings that cannot be parsed ("hello", "") produce NaN; null becomes 0; undefined becomes NaN. Because any NaN argument causes the entire result to be NaN, always validate input before passing untrusted values to Math.max().
Math.max() with no arguments
Math.max() called with no arguments returns -Infinity. This is the mathematically correct identity element for the maximum operation: when you start with -Infinity and take the max of it and any real number, you always get the real number. It also makes Math.max() safe to use as a reduce initial value without needing a special-case for empty input.
Choosing Math.max() carefully
Math.max() is best when you need the largest value from a small list of numbers or a few computed expressions. It is quick to read, but it does coerce every argument to a number first, so untrusted input should be cleaned before you pass it in. When the list is already in an array, reduce() is a safer shape for very large inputs because it avoids argument-count limits.
See Also
- Math.min() — returns the smallest of the given numbers
- Math.abs() — returns the absolute value of a number
- Math.sqrt() — returns the square root of a number