Arrays come with a useful forEach function that allows you to loop through the array.

1
2
3
4
5
6
7
8
9
var colors = ['red', 'blue', 'green'];

colors.forEach(function(color) {
  console.log(color);
});

// red
// blue
// green

You can also get the index in each loop as follows:

1
2
3
4
5
6
7
8
9
const colors = ['red', 'blue', 'green'];

colors.forEach((item, index)=>{
  console.log(index, item)
});

// 0 'red'
// 1 'blue'
// 2 'green'