Javascript Reference - JavaScript max() Method








The max() method determine which number is the largest in a group of numbers. It can accept any number of parameters.

Browser Support

max() Yes Yes Yes Yes Yes

Syntax

Math.max(n1, n2, n3,...,nX);

Parameter Values

Parameter Description
n1,n2,n3,...,nX Optional. One or more numbers to compare




Return Value

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.

Example


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.

Max/Min value for an array

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.