Javascript Reboot Lesson 3: Loops

Here’s a loop in Scratch:


Scratch loops have a “repeat” variable that counts the number of times to repeat the loop.  However, Javascript has a way of tracking this variable inside the loop, allowing you to do more things with it.

This is the syntax for a for loop:

for (var i = 0; i < 5; i++)

Each part of the for loop is executed at a specific time.  The first part is supposed to initialize your loop and gets run at the beginning of the loop.  The second part is run at the beginning of each time through the loop and exits the loop if the condition given is false.  The last part of the loop runs at the end of the loop and modifies your loop variable to the next number.

In this case, the loop variable i will start at 0, run through the loop, then go to 1, run through the loop, and so on until it hits 5.  When it goes to 5, it doesn’t run through the loop.

Again, resources for week 3:

And assignment for week 3: