Statement 1: Initializing Counter Variable
Statement 1 is used to initialize the counter variable. A counter variable is used to keep track of the number of iterations in the loop. You can initialize multiple counter variables in statement 1.
We can initialize the counter variable externally rather than in statement 1. This shows us clearly that statement 1 is optional. We can leave the portion empty with a semicolon.
Example:
let x = 2;
for (; x <= 4; x++) {
console.log("Value of x:" + x);
}
Output
Value of x:2
Value of x:3
Value of x:4
JavaScript For Loop
JavaScript for loop is a control flow statement that allows code to be executed repeatedly based on a condition. It consists of three parts: initialization, condition, and increment/decrement. This loop iterates over a code block until the specified condition is false.