index number in foreach javascript code example
Example 1: for each js
const fruits = ['mango', 'papaya', 'pineapple', 'apple'];
fruits.forEach(function(fruit){
console.log('I want to eat a ' + fruit)
});
Example 2: javascript foreach example
var colors = ["red", "blue", "green"];
colors.forEach(function(color) {
console.log(color);
});
Example 3: javascript foreach index
users.forEach((user, index)=>{
console.log(index);
});
Example 4: js get index from foreach
var myArray = [123, 15, 187, 32];
myArray.forEach(function (value, i) {
console.log('%d: %s', i, value);
});