Course contentsShow
← JavaScript
Lesson 343 of 2,72310. LoopsFree lesson

For Loop Execution Model

Understand the precise order of operations in a for loop and how each part runs.

For Loop Execution Model

You'll learn: The precise order in which JavaScript executes each part of a for loop—and why understanding this matters for avoiding bugs.

The Four-Part Sequence

A for loop has three expressions in its header, but JavaScript runs them in a specific, repeating order that's different from how they're written:

for (let i = 0; i < 3; i++) {
  console.log(i);
}

Here's what actually happens, step by step:

1. Initialization (Once Only)

let i = 0 runs first and only once, before anything else. This sets up your loop counter.

2. Condition Check (Before Each Iteration)

i < 3 is evaluated before every loop body execution. If it's falsy, the loop stops immediately—the body never runs.

3. Body Execution

console.log(i) runs if the condition was truthy.

4. Update Expression (After Each Iteration)

i++ runs after the body completes, then JavaScript jumps back to step 2 (condition check).

The Loop in Action

for (let i = 0; i < 2; i++) {
  console.log("Start:", i);
}
// Execution:
// 1. i = 0 (initialization)
// 2. Check: 0 < 2? Yes
// 3. Print "Start: 0"
// 4. i++ → i becomes 1
// 5. Check: 1 < 2? Yes
// 6. Print "Start: 1"
// 7. i++ → i becomes 2
// 8. Check: 2 < 2? No → STOP

Notice the condition check happens before the body runs each time, and the update happens after.

Key Takeaway: The for loop follows a strict cycle: initialize once → check condition → run body → update → repeat from condition check. The update never runs if the condition fails.