I know that forEach in JavaScript calls my callback function with three parameters:
arr.forEach(function callback(currentValue, index, array) {
//your iterator
})
In the above example arr
and array
are same array and arr
exists in the callback function closure.
Now the question is what is the point of passing array
to the callback function?
If your callback function were declared elsewhere:
function forEachCallback(value, i, array) {
// ...
}
Then it has no idea what array it's being used for:
someArray.forEach(forEachCallback);
Because the array is passed as the last argument, such a callback has access to it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With