JavaScript is great but sometimes it’s really annoying because it’s lack simple, built-in functions. Let’s say we have an array of the integers:
var arr = [2, 10, 30, 7, 4];
and we want to get the highest value from the array. We can of course iterate the array and check each value:
var len = arr.length,
max = 0;
for(var i=0; i<len; i++){
if(arr[i] > max) {
max = arr[i]
}
};
but it’s so much code!
So to do it in more subtle way, let’s do it in only one line of code:
Math.max.apply(Math, arr)
In JavaScript we have built-in object Math, which has a method max. This method does what it should be: count the highest value for the arguments list. The problem is that we can’t give it as an argument the array. So this will be working:
Math.max(2, 10, 30, 7, 4) // 30
but this won’t:
Math.max([2, 10, 30, 7, 4]) // NaN
So to get it around we can use apply method. Every function in JavaScript has this method built-in. It allows as to invoke function in given context with array of arguments. It’s exactly what we need here. We give as a context Math object but we can also do something like this and our example will still works:
Math.max.apply(null, arr)