The for statement establishes a loop with three optional expressions:
for (expression 1; expression 2; expression 3) { // code block to be executed } |
Expression 1 is executed once before the code block runs.
Expression 2 sets the condition for executing the code block.
Expression 3 is executed each time after the code block has completed.
for (let i = 0; i < 5; i++) { text += “The number is “ + i + “<br>”; } |
From the example above, you can interpret:
Expression 1 initializes a variable (e.g., let i = 0
).
Expression 2 sets the loop continuation condition (e.g., i < 5
).
Expression 3 updates the variable (e.g., i++
) after each iteration.