Math.max()

Math.max(value1, value2, ...values)
Returns: Number · Updated March 13, 2026 · Math
javascript math max comparison

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

ParameterTypeDefaultDescription
value1, …, valueNNumberZero 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

No arguments

Math.max();
// -Infinity

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

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

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

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

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

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

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