JavaScript Math.max() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Math

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.

JavaScript
Math.max(value1, value2, /* …, */ valueN)
CallWhat 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
JavaScript
Math.max(value1, value2, /* …, */ valueN)
JavaScript
interface Math {
  max(...values: number[]): number;
}

Parameters

ParameterDescription
value1, ..., valueNOptional. Zero or more numbers to compare. Other values are converted to numbers.

Return Value

Return ValueDescription
numberThe 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.

JavaScript
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
Try It Yourself

How It Works

  1. Pick the biggest. 9 is larger than 3 and 5.
  2. Negatives. -1 is bigger than -5.
  3. Text numbers. "7" is converted to the number 7.
  4. No values. Math.max() returns -Infinity.

Example 2 — Largest Number in an Array

Use the spread operator ... to pass array items as arguments.

JavaScript
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
Try It Yourself

How It Works

  1. Spread. ...scores turns the array into separate values.
  2. apply. Math.max.apply(null, scores) is the older way and gives the same answer.
  3. 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.

JavaScript
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
Try It Yourself

How It Works

  1. Too high. Math.min(150, 100) gives 100, then Math.max(0, 100) is 100.
  2. Too low. -20 becomes 0.
  3. In range. 42 is not changed.
  4. NaN trap. One NaN makes the whole result NaN.

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 support
Math.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().

Next: min()

Learn how to find the smallest number.

min() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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