different ways to iterate over an array JavaScript


Arrays in JavaScript can work both as a queue and as a stack. They allow you to add/remove elements both to/from the beginning or the end. and they are dynamic due that you can store values of different data types.

“The Array object, as with arrays in other programming languages, enables storing a collection of multiple items under a single variable name, and has members for performing common array operations.” by MDN

As a JavaScript developer, you will mostly work with arrays. Mastering arrays will sharpen your JavaScript skills due that arrays are an essential feature in all programming languages, not just JavaScript.

Now let’s see how we can iterate over an array’s values using JavaScript. let’s assume that we have the following array :

we iterate the index of the array using for loop starting from 0 to length of the array (arrayName.length) and access elements at each index.

an other way to iterate over an array is by using for…in , each iteration returns a key (x) · The key is used to access the value of the key

for…of is an other JavaScript feature that we can use to iterate over an array, each iteration returns a value not a key (the opposite of for..in)

The while loop loops through a block of code as long as a specified condition is true

The forEach() method calls a function for each element in an array. The forEach() method is not executed for empty elements 😉

thanks for reading this article❤️
Stay tuned for my upcoming posts where I will explain other array methods.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Abdelmalek Ennani

Abdelmalek Ennani


👨‍💻 Software engineering student 🔥 JavaScript lover ☕ ReactJs 👩‍🔧 @redux/toolkit react-refux 🧠 Laravel tinker 🔥