Looping code
Ahmed Abdel Gadir Ahmed Abdel Gadir
5 months ago
i < cats.length
The use of the i variable in loops is similar to that of its use in functions, where its defined in the block.

i.e. in this case it represents the index number in the cats array,

Note: You can find this example code on GitHub too (also see it running live).

Nested loops

Every time the outer for loop runs once, the inner for loop will run completely.
while Loops

Awesome job! for loops are great, but they have a limitation: you have to know how many times you want the loop to run. What if you want a loop to execute an unknown or variable number of times instead?

For example, if we have a deck of cards and we want to flip cards (loop a card flipping function) until we get a Spade, how could we write that in JavaScript?

That's the purpose of the while loop. It looks similar to a for loop. See the example below.
Show all 4 highlights