Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

JavaScript · Function

Math.max

Math.max() returns the greatest value among its arguments. It returns -Infinity with no arguments and NaN when any argument is or converts to NaN.

How it works

Math.max() compares zero or more numeric arguments and returns the greatest one. Arguments are converted to numbers as needed; if conversion produces NaN for any argument, the result is NaN, while calling it without arguments produces -Infinity.

Syntax

Math.max()
Math.max(value1)
Math.max(value1, value2)
Math.max(value1, value2, /* …, */ valueN)

Parameters

NameTypeDescription
value1, …, valueNnumberZero or more values whose largest numeric value is selected and returned.

Returns

The largest of the supplied values; -Infinity with no arguments, or NaN if any argument is or converts to NaN.

Key points

  • It is a static Math method that selects the greatest supplied value.
  • The function accepts no arguments or any number of candidate values.
  • An empty call uses negative infinity as its result.
  • Invalid numeric conversion propagates NaN through the result.
  • Its length property is 2, although the function actually accepts zero or more arguments.
  • Array elements can be supplied as individual arguments with spread syntax.

Common mistakes

  • A single argument that is or converts to NaN makes the entire result NaN.
  • Call it through the Math namespace; it is not an instance method and Math is not constructible.

Example: Find the largest array element

const values = [12, 7, 19, 4];
const largest = Math.max(...values);

console.log(largest); // 19

Spread the array into individual arguments so Math.max() can compare the values.

When to use it

Select the largest value

Compare several numeric candidates

Combine with spread syntax

Find an array maximum

Use reduce() instead of spreading

Process an oversized or dynamically built array

Interview questions

QWhat does Math.max() return for no arguments and for NaN input?

Math.max() returns the largest supplied value. With no arguments, it returns -Infinity; if any argument becomes NaN during numeric conversion, it returns NaN.

Follow-up: What happens with Math.max(3, undefined)? It returns NaN because undefined converts to NaN.

QHow can you find the maximum value in an array?

Use Math.max(...array) for a reasonably sized array. The spread syntax turns each element into a separate argument, matching Math.max(value1, value2, ...valueN).

Follow-up: How should a potentially huge array be handled? Iterate through it or use reduce() to avoid argument-count limits.

QAre Math.max() arguments required to already be numbers?

Math.max() accepts zero or more values, and those values are converted to numbers before comparison. Consequently, numeric strings can participate numerically, but invalid conversions can produce NaN.

Follow-up: What is Math.max("8", 3)? It returns 8 because the string is converted to the number 8.

  • Math.min()
  • Math.abs()
  • Math.floor()
  • Math.round()
  • Array.prototype.reduce()

Source: Math.max() - JavaScript | MDN Web Docs. Checked 2026-10-15.