Skip to content

Iterate over Array in JavaScript

In this tutorial we are going to discuss various ways of iterating over an array in JavaScript. This will provide you with a good understanding of exactly how to iterate over an array in JavaScript.

Let’s define an array of cities for this article.

let cities = ["Manchester", "Milan", "Moscow", "Montreal"];

Traditional for loop

for(var i=0; i < cities.length; i++) {
	console.log(cities[i]);
}

//Manchester
//Milan
//Moscow
//Montreal

Array forEach

cities.forEach(city => {
    console.log(city);
})

//Manchester
//Milan
//Moscow
//Montreal

Array Map()

cities.map(city => console.log(city));

//Manchester
//Milan
//Moscow
//Montreal
See also  Check if a variable is a date in JavaScript?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.