Javascript foreach loop
The JavaScript forEach() method is an Array method that executes a provided function once for each element in the array. The forEach() method does not return anything.
Syntax:
array.forEach(function(currentValue, index, arr), thisValue)
Parameters:
currentValue: The value of the current element being processed in the array.
index: The index of the current element being processed in the array.
arr: The array that forEach() is being applied to.
thisValue (Optional): A value to be passed to the function to be used as its “this” value.
Example 1:
let arr = [1, 2, 3];
arr.forEach(element => {
console.log(element);
});
// Output
// 1
// 2
// 3
Example 2:
var items = ["apple", "orange", "banana"];
// loop through the array
for (var i = 0; i < items.length; i++) {
console.log(items[i]);
}