Encodera

Thanks for reading!

Donate to the site — we work hard for you.

JavaScript reference

Math.max

Returns the largest of the given numbers.

Available since: ES1

Syntax

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

Parameters

ParameterTypeDescription
value1, …numberThe numbers to compare.

Return value

numberThe largest of the numbers passed.

Examples

Several arguments:

console.log(Math.max(1, 5, 3));

Output:

5

To pass an array, spread it with ...:

const numbers = [2, 8, 4];
 
console.log(Math.max(...numbers));
console.log(Math.min(...numbers));

Output:

8
2

Notes

With no arguments Math.max() returns -Infinity — which is what happens when you spread an empty array.

See also

Related recipes

In the tutorial

  • Numbers — Numbers in JavaScript — integers and fractions, rounding, the Math object, toFixed and the special values Infinity and NaN.

Thanks for reading!

Donate to the site — we work hard for you.