forEach() method
How does the forEach() method index the elements in an array? For instance if I have an array and I want to iterate over it and show the index of each array item I can do it with a for() method loop like this: var fruits = ["Apples", "Bananas", "Cherries"]; //using for loop to iterate over the fruits array and showing their index value. for(var i = 0; i < fruits.length; i++) { document.write(i + ": " + fruits[i] + "<br>"); } OUTPUT: 0: Apples 1: Bananas 2: Cherries //I can also do this using the forEach() method like this: fruits.forEach(function(fruitsItem, index, fruits) { document.write(index + ": " + fruitsItem + "<br>"); }); This will give same output but in the for() method I had to initialize the i variable with 0 to give the desired output but I don't need to the same with the forEach() method. How does this works?