How to find maximum value in array using JavaScript

I have been using some JavaScript techniques in the past to find the maximum number in an array. I recently stumbled upon a real fast and simple solution to find the max number in an array and thought of sharing it with all of you. Let me know what do you think of this method.

var array =  [ "17", "15", "21", "5", "25", "10", "20" ];
var maxvalue  = Math.max.apply(null, array);
console.log(maxvalue);		

Output

25

Another Example

var array =  [10,3,2,5,9,20];
var maxvalue  = Math.max.apply(null, array);
console.log(maxvalue);

Output

20

Leave a Reply

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