Javascript\'s array iteration functions (forEach
, every
, some
etc.) allow you to pass three arguments: the current item, the current i
It is possible that you want to pass a generic function as an argument to forEach
and not an anonymous function. Imagine a situation where you have a function defined like that:
function my_function(item, i, arr) {
// do some stuff here
}
and then use it on different arrays:
arr1.forEach(my_function);
arr2.forEach(my_function);
The third argument allows the function to know which array is operating on.
Another case where this might be usefull, is when the array has not been stored in a variable and therefore does not have a name to be referenced with, e.g.:
[1, 2, 3].forEach(function(item, i, arr) {});