👋 Hello! I'm Alphonsio the robot. Ask me a question, I'll try to answer.
How to iterate through JavaScript arrays

To iterate through JS arrays, you have 3 options :

  • Sequential for loop
  • Array.prototype.forEach
  • ES6 for-of statement


Sequential for loop

var myArray = ['First', 'Second', 'Third'];
for (var i = 0; i < myArray.length; i++) {
	// Do awesome stuff here
    console.log(myArray[i]);
}


Array.prototype.forEach

var myArray = ['First', 'Second', 'Third'];
myArray.forEach(function (value, index) {
	// Do awesome stuff here
    console.log(index, value);
})

ES6 for-of statement

var myArray = ['First', 'Second', 'Third'];
for (const value of myArray){
	// Do awesome stuff here
    console.log(value);
}

More