Iterate on an Array with Index in Javascript


In javascript using forEach loop you can iterate on an array with index. You can check the following example where we are iterating on colors array with index.

var colors = ['red', 'green', 'blue'];

colors.forEach(function(v, i){
  console.log('Value at '+ i +' is: ' + v);
});

Output:

Value at 0 is: red
Value at 1 is: green
Value at 2 is: blue