< Back to JavaScript notes

Loops

Different JavaScript loops and when to use them

Resources:


While/Do Loops

Sometimes code must be repeated so often that the above approach won’t work. While loops make this easier, as they constantly repeat code under a certain condition. Logging all number in a certain range (between 10 and 100) works this way:

let number = 10;

while (number <= 100) {
  console.log(number);
  number = number + 2;
}

Notice how this demonstrates how binding can control the execution of a code.

Do loops are the same, but are set up so they execute their code at least once. In the below example, even if number is set 100, it will still run at least once.

let number = 100;

do {
  console.log(number);
  number = number + 2;
} while (number <= 100)

Make sure the conditions in a While/Do loop resolve! If they continue infinitely they will crash your program.

For Loops

For Loops do the same basic functions as While/Do loops, but give you more control over the iterations. You must set:

The example below using the loop variable in all three areas, but note that anything else could be used if wanted.

let result = 1;
for (let counter = 0; counter < 10; counter = counter + 1) {
  result = result * 2;
}
console.log(result);
// → 1024

Loop increments can be simplified in two ways:

There are two ways to adjust control of a loop: