Math.max
Returns the largest of the given numbers.
Available since: ES1
Syntax
Math.max(value1, value2, /* … */)Parameters
| Parameter | Type | Description |
|---|---|---|
| value1, … | number | The numbers to compare. |
Return value
numberThe largest of the numbers passed.
Examples
Several arguments:
console.log(Math.max(1, 5, 3));Output:
5To pass an array, spread it with ...:
const numbers = [2, 8, 4];
console.log(Math.max(...numbers));
console.log(Math.min(...numbers));Output:
8
2Notes
With no arguments Math.max() returns -Infinity — which is what happens when you spread an empty array.
See also
- Math.round — Rounds a number to the nearest integer.
- Array.prototype.reduce — Reduces an array to a single value.
- Math.min — Returns the smallest of the given numbers.
- Spread syntax — Three dots spread an array or object into separate items.
Related recipes
In the tutorial
- Numbers — Numbers in JavaScript — integers and fractions, rounding, the Math object, toFixed and the special values Infinity and NaN.