1const avengers = ['thor', 'captain america', 'hulk'];
2avengers.forEach((item, index)=>{
3 console.log(index, item)
4})
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});
1let words = ['one', 'two', 'three', 'four'];
2words.forEach((word) => {
3 console.log(word);
4});
5// one
6// two
7// three
8// four
1users.forEach((user, index)=>{
2 console.log(index); // Prints the index at which the loop is currently at
3});
1let numbers = ['one', 'two', 'three', 'four'];
2
3numbers.forEach((num) => {
4 console.log(num);
5}); // one //two //three // four
6
1const array1 = ['a', 'b', 'c'];
2
3array1.forEach(element => console.log(element));
4
5// expected output: "a"
6// expected output: "b"
7// expected output: "c"
8