Loops

Loops are repetitive conditions where one variable in the loop changes. Loops are handy, if you want to run the same code over and over again, each time with a different value.

Instead of writing:

  1. doThing(cars[0]);
  2. doThing(cars[1]);
  3. doThing(cars[2]);
  4. doThing(cars[3]);
  5. doThing(cars[4]);

You can write:

  1. for (var i=0; i < cars.length; i++) {
  2. doThing(cars[i]);
  3. }