1const avengers = ['thor', 'captain america', 'hulk'];
2avengers.forEach((item, index)=>{
3 console.log(index, item)
4})
1users.forEach((user, index)=>{
2 console.log(index); // Prints the index at which the loop is currently at
3});
1const iterable = [...];
2for (const [index, elem] in iterable.entries()) {
3 f(index, elem);
4}
5
6// or
7iterable.forEach((elem, index) => {
8 f(index, elem);
9});
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});
1let numbers = ['one', 'two', 'three', 'four'];
2
3numbers.forEach((num) => {
4 console.log(num);
5}); // one //two //three // four