DEV Community

Cover image for 7 Ways of looping through an array in JavaScript.
Peter Wainaina
Peter Wainaina

Posted on

7 Ways of looping through an array in JavaScript.

Simply put, an array is a collection of same data type items stored in contiguous(consecutive; following each other) memory locations.

More often than not, arrays are used during programming for a couple of reasons: storing user information, storing some identification attributes, just to mention but a few.

It becomes necessary at some point in time for a programmer to access this stored data and I have highlighted 7 ways in which one can easily do so using some JavaScript code snippets:

  1. Looping through an array using for loop.

Image description

  1. Looping through an array using while loop.

Image description

  1. Looping through an array using do...while loop.

Image description

  1. Looping through an array using map() method.

Image description

  1. Looping through an array using for...of.

Image description

  1. Looping through an array using for...in.

Image description

  1. Looping through an array using forEach() method.

Image description

I hope the article was helpful. Happy coding!

Top comments (0)