Skip to content

Find the maximum and minimum value in a JavaScript array

To find the maximum and minimum value in JavaScript array we can use Math.min.apply() and Math.max.apply().

Advertisements
var arr = [20, 10, 5, 15];
Math.max.apply(null, arr); //20
Math.min.apply(null, arr); //5
Advertisements

Math.min(…array) amd Math.max(…array) is another option but we can’t use this with larger arrays because of slow performance. These method works with arguments, not arrays. We use the spread operator … to convert array to arguments.

var arr = [20, 10, 5, 15];
Math.max(...arr); //20
Math.min(...arr); //5
, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.