Find the smallest number in an array with JavaScript

Let’s say you’re given an array of numbers, and you’re required to find the smallest number in the array. How would you do it?

Though I didn’t find this challenge difficult to complete, my solution looked like a Toyota Prius next to the Tesla I saw on Codewars today.

Here’s what I would have done normally:

And this is what I saw on Codewars today:

Edge cases like an empty array and arrays with elements other than Number type aside (which neither solutions bother themselves with), this second solution is superior in almost every way to mine.

Isn’t that a nice looking line of code, Math.min.apply(null, numbers)?

It’s pretty to look at and very easy to understand. Math.min(a, b, c[, ...]) normally returns the lowest value among all the arguments passed into it, but the problem is that it doesn’t accept arrays. That’s where .apply comes into play.

function.apply(thisArg, [argsArray]) accepts two arguments. thisArg is for explicitly stating which object this should refer to. [argsArray] is whatever arguments you’d like to pass into the original function in the format of an array.

The clever part is combining the power of two in-built JavaScript methods (both available in ECMAScript 5) to carry out this operation. The only caveat is the length of the array cannot exceed 65536, as in [1,2,3,…,65535,65536]. It’s likely an arbitrarily set figure, but with good reason. Stack overflow?

To find the max number, just do Math.max.apply!

More on the apply method at MDN.

UPDATE: With ECMAScript 2015 (ES6), it’s become slightly easier to achieve the same thing. The big difference is how clean it looks in ES6 using the ‘spread’ (...) operator: