Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Math.max: largest value

Math.max converts every argument to Number and returns the largest resulting value.

Version: ECMAScript 1 onward; example target: Node.js 24.22.2

Syntax

Math.max(...args)

Math.max(...args) first applies ToNumber to every argument in order, then selects the largest result. With no arguments it returns -Infinity, which acts as the identity for finding a maximum. If any converted argument is NaN, the result is NaN; importantly, all conversions occur before that test.

Numeric strings and other convertible values are accepted, while a Symbol or BigInt throws TypeError. Objects can run valueOf or toString, and a conversion error stops the call. When the candidates are the two zeros, +0 is considered larger than -0; Infinity is larger than every finite Number.

The function has existed since ECMAScript 1 and is not a constructor. For a small array, Math.max(...values) is practical. Do not spread a very large collection because argument limits are engine-dependent; use a loop or reduce and deliberately define empty-input and NaN handling.

Examples

const scores = [-4, 12, 7];
console.log(Math.max(...scores));
console.log(Math.max(), Number.isNaN(Math.max(3, undefined)));
console.log(Object.is(Math.max(-0, +0), +0));
console.log(Math.max("6", 2));
try { Math.max(1n, 2); } catch (error) { console.log(error instanceof TypeError); }
const seen = [];
const a = { valueOf() { seen.push("a"); return NaN; } };
const b = { valueOf() { seen.push("b"); return 4; } };
console.log(Number.isNaN(Math.max(a, b)), seen.join(","));

Result

12
-Infinity true
true
6
true
true a,b

Math.max → 12 and ordered coercion a,b

Sources