JavaScript Math.max() Explained with Examples

Math Max

Math.max() is a function that returns the largest value from a list of numeric values passed as parameters. If a non-numeric value is passed as a parameter, Math.max() will return NaN .

An array of numeric values can be passed as a single parameter to Math.max() using either spread (...) or apply . Either of these methods can, however, fail when the amount of array values gets too high.


Math.max(value1, value2, value3, ...);


Numbers, or limited array of numbers.

Return Value

The greatest of given numeric values, or NaN if any given value is non-numeric.


Numbers As Parameters

Math.max(4, 13, 27, 0, -5); // returns 27

Invalid Parameter

Math.max(4, 13, 27, 'eight', -5); // returns NaN

Array As Parameter, Using Spread(…)

let numbers = [4, 13, 27, 0, -5];

Math.max(...numbers); // returns 27

Array As Parameter, Using Apply

let numbers = [4, 13, 27, 0, -5];

Math.max.apply(null, numbers); // returns 27