Click here to navigate the course.

Drag the edges to resize the window.

In Projects, you can keep track of your progress as you go throught the tasks. Check each item as you complete it!

Code Editor

Instead of writing out the same code over and over, let’s make the computer loop through our array for us. We can do this with for loops.

The syntax looks like this:

var animals = ["Grizzly Bear", "Sloth", "Sea Lion"]; for (var i = 0; i < animals.length; i++) { console.log(animals[i]); } // Output: // Grizzly Bear // Sloth // Sea Lion

Since this syntax is a little complicated, let's break it into 4 parts:

  1. Within the for loop's parentheses, the start condition is var i = 0, which means the loop will start counting at 0.

  2. The stop condition is i < animals.length, which means the loop will run as long as i is less than the length of the animals array. When i is greater than the length of the animals array, the loop will stop looping.

  3. The iterator is i++. This means that each loop, i will have 1 added to it.

  4. And finally, the code block is inside the { ... }. The block will run each loop, until the loop stops.

The secret to loops is that i, the variable we created inside the for loop's parentheses, is always equal to a number. To be more clear, the first loop, i will equal 0, the second loop, i will equal 1, and the third loop, i will equal 2.

This makes it possible to write animals[0], animals[1], animals[2] programmatically instead of by hand. We can write a for loop, and replace the hard coded number with the variable i, like this: animals[i].

report a_bug
If you see a bug or any other issue with this page, please report it here.