Javascript - Math min() and max() Methods


The min() and max() methods determine which number is the smallest or largest in a group of numbers.

These methods accept any number of parameters:

var max = Math.max(3, 5, 2, 6);

var min = Math.min(3, 4, 2, 6);

You can use these methods to avoid extra loops and if statements to determine the smallest or largest value out of a group of numbers.

To find the maximum or the minimum value in an array, use the apply() method as follows:

var values = [1, 2, 3, 4, 5, 6, 7, 8];
var max = Math.max.apply(Math, values);