Getting maximum and minimum

suggest change

The Math.max() function returns the largest of the numbers you pass. With no arguments it returns -Infinity.

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

The Math.min() function returns the smallest of the numbers you pass. With no arguments it returns Infinity.

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

Getting maximum and minimum from an array:

const 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

Spread syntax does the same job. Spreading or applying a very large array can throw a RangeError when the argument list is too long.

const 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

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents