Iterating Over Collections

One of the most common uses of a for loop is to carry out a task once for each item in a collection. We have learned about two types of collections, strings and arrays. When using a loop with a collection in this way, we say that the loop iterates over the collection.

Iterating Over Strings

The following example prints each of the characters of the string "LaunchCode" on a separate line.

Example
1
2
3
4
5
let name = "LaunchCode";

for (let i = 0; i < name.length; i++) {
   console.log(name[i]);
}

Console Output

L
a
u
n
c
h
C
o
d
e

Since name.length is 10, the loop executes once each for the values of i from 0 to 9. The loop body, console.log(name[i]);, will print name[i] each time. In each case, name[i] is one of the characters of name.

Example

In your copy of javascript-projects, open up loops/chapter-examples. Inside you will find for-Loop-Practice-With-Strings.js. In this file, try writing a program that prints each character of your name on a different line.

// create a string variable containing your name

// write a for loop that prints each character in your name on a different line

Iterating Over Arrays

The following example prints each of the programming languages in the array languages on a separate line.

Example
1
2
3
4
5
let languages = ["JS", "Java", "C#", "Python"];

for (let i = 0; i < languages.length; i++) {
   console.log(languages[i]);
}

Console Output

JS
Java
C#
Python

Similar to the string example, this loop executes 4 times because languages.length is 4. For each iteration, languages[i] is one of the items in the array and the given language is printed.

Example

Inside chapter-examples in the loops directory in javascript-projects, you will find for-Loop-Practice-With-Arrays.js. In this file, try writing a program that prints the name of each member of your family on a different line.

1
2
3
// create an array variable containing the names

// write a for loop that prints each name on a different line