JavaScript Getting maximum and minimum


Example

The Math.max() function returns the largest of zero or more numbers.

Math.max(4, 12);   //  12
Math.max(-1, -15); // -1

The Math.min() function returns the smallest of zero or more numbers.

Math.min(4, 12);   //  4
Math.min(-1, -15); // -15

Getting maximum and minimum from an array:

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

console.log(max); // Logs: 9
console.log(min); // Logs: 1

ECMAScript 6 spread operator, getting the maximum and minimum of an array:

var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9],
    max = Math.max(...arr),
    min = Math.min(...arr);

console.log(max); // Logs: 9
console.log(min); // Logs: 1