1users.forEach((user, index)=>{
2 console.log(index); // Prints the index at which the loop is currently at
3});
1///Simple One
2var a = ["a", "b", "c"];
3a.forEach(function(entry) {
4 console.log(entry);
5});
6
7
8///Function concept
9
10var fruits = ["apple", "orange", "cherry"];
11fruits.forEach(myFunction);
12
13function myFunction(item, index) {
14 document.getElementById("demo").innerHTML += index + ":" + item + "<br>";
15}
16
1const array1 = ['a', 'b', 'c'];
2
3array1.forEach((element, index) => console.log(element, index));
1var myArray = [123, 15, 187, 32];
2
3myArray.forEach(function (value, i) {
4 console.log('%d: %s', i, value);
5});
6
7// Outputs:
8// 0: 123
9// 1: 15
10// 2: 187
11// 3: 32
12