JavaScript Math.max() Method

Definition
Math.max(...values) returns the largest of the numbers you give it. Pass any number of values, such as Math.max(3, 9, 5), which returns 9.
Math is built into JavaScript. Call the method directly. It is not a constructor, so never write new Math().
Syntax
Call Math.max() like this. Start with this line.
Math.max(value1, value2, /* …, */ valueN)
| Call | What it does |
|---|
Math.max(value1, value2, ...) | Returns the largest of the given numbers. |
Math.max() | With no arguments, returns -Infinity. |
Built into JavaScript. Call Math.max() on the Math object — no import required.
Library declarations
Math.max(value1, value2, /* …, */ valueN)
interface Math {
max(...values: number[]): number;
}
Parameters
| Parameter | Description |
|---|
| value1, ..., valueN | Optional. Zero or more numbers to compare. Other values are converted to numbers. |
Return Value
| Return Value | Description |
|---|
| number | The largest value. Returns -Infinity if there are no arguments, and NaN if any argument is NaN or cannot be turned into a number. |
JavaScript Examples
Example 1 — Basic Use
Pass numbers one by one. You get the biggest.
console.log(Math.max(3, 9, 5)); // 9
console.log(Math.max(-1, -5)); // -1
console.log(Math.max(2, "7")); // 7
console.log(Math.max()); // -Infinity
How It Works
- Pick the biggest.
9 is larger than 3 and 5. - Negatives.
-1 is bigger than -5. - Text numbers.
"7" is converted to the number 7. - No values.
Math.max() returns -Infinity.
Example 2 — Largest Number in an Array
Use the spread operator ... to pass array items as arguments.
var scores = [72, 95, 88, 61];
console.log(Math.max(...scores)); // 95
console.log(Math.max.apply(null, scores)); // 95
var best = scores.indexOf(Math.max(...scores));
console.log(best); // 1
How It Works
- Spread.
...scores turns the array into separate values. - apply.
Math.max.apply(null, scores) is the older way and gives the same answer. - Find the position.
indexOf of the max tells you where the best score is.
Example 3 — Clamp a Value and Watch for NaN
Use max and min together to keep a number between two limits.
function clamp(value, low, high) {
return Math.max(low, Math.min(value, high));
}
console.log(clamp(150, 0, 100)); // 100
console.log(clamp(-20, 0, 100)); // 0
console.log(clamp(42, 0, 100)); // 42
console.log(Math.max(1, NaN, 3)); // NaN
How It Works
- Too high.
Math.min(150, 100) gives 100, then Math.max(0, 100) is 100. - Too low.
-20 becomes 0. - In range.
42 is not changed. - NaN trap. One
NaN makes the whole result NaN.
Compatibility
Browser & Runtime Support
Math.max() has been part of JavaScript since ES1 and is Baseline Widely available everywhere.
✓ Baseline · ES1
Math.max()
Supported in every browser and Node.js version. No polyfill is needed.
99% Modern browser support
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full supportMath.max Excellent
Bottom line: Use Math.max() freely. It works in all browsers and runtimes.
Frequently Asked Questions
Math.max(a, b, c) returns the largest number from its arguments. Math.max(3, 9, 5) is 9.
Use the spread operator: Math.max(...numbers). The older way is Math.max.apply(null, numbers).
It returns -Infinity. This is because every number is bigger than -Infinity.
The result is NaN. Remove or fix bad values before you call Math.max().
Combine max and min: Math.max(0, Math.min(value, 100)).
Spreading a huge array (hundreds of thousands of items) can throw an error. For huge arrays, use a loop or reduce().
About the author
Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
12 people found this page helpful