1const avengers = ['thor', 'captain america', 'hulk'];
2avengers.forEach((item, index)=>{
3 console.log(index, item)
4})
1var colors = ['red', 'blue', 'green'];
2
3colors.forEach(function(color) {
4 console.log(color);
5});
1const fruits = ['mango', 'papaya', 'pineapple', 'apple'];
2
3// Iterate over fruits below
4
5// Normal way
6fruits.forEach(function(fruit){
7 console.log('I want to eat a ' + fruit)
8});
1const array1 = ['a', 'b', 'c'];
2
3array1.forEach(element => console.log(element));
1var colors = ["red", "blue", "green"];
2colors.forEach(function(color) {
3 console.log(color);
4});
1var arr = [1, 2, 3, 4, 5, 6];
2
3//var sum = 0;
4arr.forEach(function (value, index, array){ // Built in method
5 //sum += value;
6 console.log(value, index, array);
7})
8
9//console.log(sum); // We can print sum of all Array element (if those are numbers)
10
11// function forEach(arr, cb){ // How forEach() method works
12// for (var i = 0; i<arr.length; i++){
13// cb(arr[i], i, arr);
14// }
15// }
16// var sum = 0;
17// forEach(arr, function (value, index, arr){
18// console.log(value, index, arr);
19// sum += value;
20// })
21// console.log(sum);