While Loops With Arrays

While loops can be used to iterate over every element in an array:

var animals = ["cats", "dogs", "sheep"];
var counter = 0;
while (counter < animals.length) {
  console.log("I like " + animals[counter]);
  counter++;
}

This loop will print the following strings to the console:

I like cats
I like dogs
I like sheep

The counter variable is used as the index for the different elements in the animals array.

animals[0]; // "cats"
animals[1]; // "dogs"
animals[2]; // "sheep"

A while loop can also be used to sum all the elements in an array of numbers.

var numbers = [10, 30, 50];
var sum = 0;
var i = 0;
while (i < numbers.length) {
  sum = sum + numbers[i];
  i++;
}
console.log(sum); // 90

The result 90 is printed to the console because 10 + 30 + 50 = 90. Let's look at how the loop accomplishes this in three steps:

Loop 1: sum = 0 + 10 = 10; Loop 2: sum = 10 + 30 = 40; Loop 3: sum = 40 + 50 = 90;

On the first loop, the code takes the variable sum (0) and adds the first element of the array, numbers[0] (10). In the first loop, the variable sum receives the new value 0 (current value of sum) + 10 = 10. This is repeated for loops two (sum = 10 + 30 = 40) and three (sum = 40 + 50 = 90). By the time the loop ends, the new value of sum is 90. This is what's printed to the console.

Question Click to View Answer

What does the following code print to the console?

var colors = ["red", "blue"];
i = 0;
while (i < colors.length) {
  console.log(colors[i] + " is pretty")
  i++;
}
red is pretty
blue is pretty

This loop iterates through every element in the colors array and prints them to the console.

What does the following code print to the console?

var funNumbers = [2, 4, 6];
result = 0;
i = 0;
while (i < funNumbers.length) {
  result = result + funNumbers[i];
  i++;
}
console.log(result);

12

This loop sums every number in the funNumbers array.

What does the following code print to the console?

var yummy = ["chi", "mi", "changa"];
result = "";
i = 0;
while (i < yummy.length) {
  result = result + yummy[i];
  i++;
}
console.log(result);

chimichanga

This loop concatenates every string in the yummy array into a single string. The string "chimichanga" is printed to the console.

<-- Previous (Infinite Loops) Next (More Arrays) -->