forEach loop on array with fat arrow forEach loop on array with fat arrow arrays arrays

forEach loop on array with fat arrow


It seems that this code here:

[1,2,3].forEach(console.log)

is the same as:

[1,2,3].forEach((value, index, array) => console.log(value, index, array))

It is not "wrong", it is just "unusual" in comparison to Scala or Java (method references) which seem to support method references with one single parameter. Javascript just seem to copy all parameters across to the referenced method (e.g. console.log) and if this method supports varargs, everything gets processed.

But if you do not like this behaviour you can fix it in Javascript. Create a simple function which accepts one single parameter:

function print(t) { console.log(t) }

and then execute:

[1,2,3].forEach(print)

And this prints the result, that will let you feel at home if you are coming from a Scala background:

123


You can use the ES6 'fat' arrow Like this.

     const data = [1,2,3,4]     data.forEach(item => {         console.log(item)     })


forEach passes multiple arguments to the callback, not just one: The item, its index, and the object on which forEach was called. Most implementations of console.log accept multiple parameters and output all of them. That's why forEach(console.log) outputs the entry (1), its index (0), and the array ([ 1, 2, 3 ]), then the next, etc.

With the arrow function, you're only using the first of those arguments.


Side note: forEach(console.log) also passes in console.log without ensuring that this during the call to log will be console. Some console implementations don't care, others do. So if you wanted that second form, you'd probably be better off with forEach(console.log, console).