Monica Monica - 1 year ago 60
Javascript Question

What is the difference between these two JS functions?

I currently have 2 functions that do the same thing, which is a function named numbers that returns true if all the parameters that are passed are of the Number type. Otherwise, the function should return false. The function should accept any number of parameters.

So I did the following function:

var numbers = function(numList) {
for(var i = 0; i < numList.length; i++) {
if ( typeof numList[i] !== 'number' ) {
return false;
return true;

But later on, I found this other solution.

var numbers = function() {
return, function(argument) {
return typeof argument !== 'number';
}).length === 0;

The last solution is more advanced though, but I want to know pros and cons of these 2 functions and also, which one is better? Maybe my solution will cause some errors at some point?

Answer Source

The first function shortcuts, i.e. it returns false as soon as it encounters a non-number. The second function always processes all inputs and decides at the end.

The first function is much more efficient.