Annotary
Sort

Looping code

developer.mozilla.org
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,
Cancel

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

Cancel
crash.
Nested loops


Every time the outer for loop runs once, the inner for loop will run completely.
Cancel
p.
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.
Cancel
Show all 4 highlights
Loading...