If you want to repeat code, then you would use a loop.
If you want to do something a set amount of times, use a for loop.
i
is the name for our counter. The counter starts at zero and increases by one every
time it completes a loop.
for (var i = 0; i < 3; i++) {
alert("Loop number " + i);
}
If you want to keep something running until something changes, use a while loop.
var i = true;
while (i) {
i = false;
}
Beware of the infinite loop
var i = true;
while (i) {
// this will never stop repeating
i = true;
}