-3

The book that I'm studying says about iterating over arrays with every that:

The function these methods use must follow one rule—it must accept three arguments like the following code:

function functionName(value, index, array) { 
// do something here
}

Does that mean that I must always use 3 arguments? If so then why does this code work?

var numbers = [ 1, 2, 2 ];
function isLessThan3(value) {
  var returnValue = false;
  if (value < 3) {
    returnValue = true;
}
return returnValue; }

document.write(numbers.every(isLessThan3));
4
  • 3
    As a suggestion stop reading that book. "it must accept three arguments ..." is misleading.
    – Ram
    Commented Aug 26, 2016 at 21:08
  • Please be specific what you are asking. That function does not have the code inside. It is just for explaination. And you should learn what is array first. Commented Aug 26, 2016 at 21:10
  • 1
    Could you give more context about the quote from the book? What methods is it actually talking about when it says "these methods"?
    – Addison
    Commented Aug 26, 2016 at 21:11
  • 1
    Next time, read the MDN page about it, it explains it why developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
    – Braiam
    Commented Aug 26, 2016 at 21:41

3 Answers 3

1

There is no limitation on how many arrguments you can put in a function with Javascript.

you have a very good explenation about this topic in the next answer by @Niet the Dark Absol https://stackoverflow.com/a/22747272/1283672

i believe that the book was reffering to something more specific within it's scope.

And just to be clear you can put no arrgs in a function either.

0

It's a bit ugly, the code, you have, but there is help. You might use the following without a temporary variable. Just return the result of the comparison.

function allLessThan3(value) {
    return value < 3;
}

var numbers = [1, 2, 2];
console.log(numbers.every(allLessThan3));

-1

No, you can use from 0 to 3 arguments

10
  • I can pass any number of arguments I like to a function... Commented Aug 26, 2016 at 21:11
  • @JanDvorak yes, but in this case 4th and greater arguments gonna be undefined Commented Aug 26, 2016 at 21:14
  • What do you mean by that? Commented Aug 26, 2016 at 21:15
  • @JanDvorak I mean if you execute this: ['a','b','c'].every(function(x,y,z,a,b,c) {console.log(a,b,c)}) you will see three undefined. Commented Aug 26, 2016 at 21:17
  • 1
    @FedeSc Are you saying that the answer says "a function that names three slots for pieces of data can make use of three pieces of data"? Even if that was what the answer meant - and people were expected to interpret it that way - how is that ever useful to anyone? Commented Aug 26, 2016 at 21:46

Not the answer you're looking for? Browse other questions tagged or ask your own question.