Describing the forEach
Operation
- The
forEach
method is used for looping over an array -
It accepts a callback function with the following arguments:
currentValue:
Current element processed in the arrayindex
: Optional index ofcurrentValue
array
: Optional array that is calledthisArg
: Optional value to use asthis
Defining an Array
let fruits = ['Apple', 'Banana'];
console.log(fruits.length);
// 2
Using the forEach
Operation
// Regular function
fruits.forEach(function(item, index, array) {
console.log(item, index);
})
// Apple 0
// Banana 1
// Arrow function
fruits.forEach(item => console.log(item));
// Apple
// Banana
Next