The max()
method determine which number is the largest in a group of numbers.
It can accept any number of parameters.
max() |
Yes | Yes | Yes | Yes | Yes |
Math.max(n1, n2, n3,...,nX);
Parameter | Description |
---|---|
n1,n2,n3,...,nX | Optional. One or more numbers to compare |
The highest number of the arguments.
It returns -Infinity if no arguments are given.
It returns NaN if one or more arguments are not numbers.
var max = Math.max(3, 5, 3, 6);
console.log(max); // 6
var min = Math.min(3, 5, 3, 6);
console.log(min); //3
The code above generates the following result.
To find the maximum or the minimum value in an
array, you can use the apply()
method as follows:
var values = [1, 2, 3, 4, 5, 6, 7, 8];
var max = Math.max.apply(Math, values);
console.log(max);//8
The code above generates the following result.