Essential JavaScript

Looping Over Arrays

We can take action on each element in an array by using a for loop.

This works much the same as iterating over a string:

const planets = [
  'Mercury',
  'Venus',
  'Earth',
  'Mars',
  'Jupiter', 
  'Saturn',
  'Uranus',
  'Neptune',
  'Pluto', // for old times' sake
];
for (let i = 0; i < planets.length; i++) {
  console.log(planets[i]);
}

Learning Goals

Code Editor