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.
L a u n c h C o d e
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
In your copy of
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.
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.
chapter-examples in the
loops directory in
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.