JavaScript: How To Pass An Array To Math.max?

Umar Ashfaq
Eastros
Published in
1 min readSep 8, 2012

Math.max is a very handy function in JavaScript to quickly find maximum of given numbers. The function takes a variable no. of arguments which may range from one to as much as you like.

[sourcecode language=”javascript”]
Math.max(d1, d2, d3, … , di);
[/sourcecode]
But what would you do if you have your numbers stored in an array and you want to find maximum of them? Math.max doesn’t take array as an argument.
If you have any ideas please share below in comments. I came up with this quick hack:
[sourcecode language=”javascript”]
Math.max.apply(Math, your_array_here);
[/sourcecode]
You can try this hack on any function that takes arguments in similar manner. Enjoy!

--

--