JavaScript: iterate through Arrays with for statements


The 3 for statements

There are 3 ways to iterate through an array with for loops:

  1. With the simple for loop.
  2. With the for...of loop, which works with any iterable object. Arrays are iterable objects.
  3. With the loop, which works with any object. Arrays are objects that happen to have integer-like strings as keys (or property names).

Note that the for...of statement iterates through the elements of the array while the` statement iterates through the property names (or keys) of the array, which are its indices.


const array = [1, 2, 3, 4, 5];

// Simple for loop
for (let i = 0; i < array.length; i += 1) {

// for...of loop
for (let num of array) {

// loop
for (let i in array) {

Practice and remember

You can now test you level of expertise with JS arrays.