Describing the forEach Operation
- The
forEachmethod 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 ofcurrentValuearray: Optional array that is calledthisArg: Optional value to use asthis
Defining an Array
let fruits = ['Apple', 'Banana'];
console.log(fruits.length);
// 2Using 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
// BananaNext