Understand Loops
Loops
Loops run several times.
While loop
The while loop iterates as long as a condition is true.
let i = 0;while (i < 9) {console.log('This piece of code ran. i equals: ' + i);i++;}
Do...while loop
The do...while loop iterates as long as a condition is true. The difference between the while loop and do...while loop is the statement is executed once before the condition is checked.
let i = 0;do {console.log('This piece of code ran. i equals: ' + i);i++;} while (i < 9)
For loop
The for loop has a concise syntax. In between the parentheses there are three things that happen: a variable is declared, the condition is checked, and finally a change is made to the variable (this happens each time after the code within the code block is run).
for (let i = 0; i < 9; i++) {console.log('This piece of code ran. i equals: ' + i);}