0
0
Javascriptprogramming~10 mins

Nested loops in Javascript - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Nested loops
Start Outer Loop i=0
Start Inner Loop j=0
Execute Inner Loop Body
Update j
Check j < limit?
YesRepeat Inner Loop
No
Update i
Check i < limit?
YesRepeat Outer Loop
No
End
The outer loop starts and for each iteration, the inner loop runs completely before the outer loop moves to the next iteration.
Execution Sample
Javascript
for (let i = 0; i < 2; i++) {
  for (let j = 0; j < 3; j++) {
    console.log(`i=${i}, j=${j}`);
  }
}
This code prints pairs of i and j values where i runs from 0 to 1 and j runs from 0 to 2 for each i.
Execution Table
StepijCondition i<2Condition j<3ActionOutput
100truetruePrint i=0, j=0i=0, j=0
201truetruePrint i=0, j=1i=0, j=1
302truetruePrint i=0, j=2i=0, j=2
403truefalseInner loop ends, increment i
510truetruePrint i=1, j=0i=1, j=0
611truetruePrint i=1, j=1i=1, j=1
712truetruePrint i=1, j=2i=1, j=2
813truefalseInner loop ends, increment i
920falseN/AOuter loop ends
💡 Outer loop condition i<2 is false when i=2, so loops stop.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6After 7Final
i000011112
j01230123N/A
Key Moments - 3 Insights
Why does the inner loop run completely for each single iteration of the outer loop?
Because the inner loop is inside the outer loop's block, it finishes all its iterations before the outer loop increments i (see execution_table rows 1-4 and 5-8).
Why does j reset to 0 each time the outer loop increments i?
Because j is declared inside the inner loop, it starts fresh at 0 for each new outer loop iteration (see variable_tracker showing j resets at After 4 and After 8).
What causes the loops to stop running?
The outer loop stops when i reaches 2 (condition i<2 is false at step 9), which ends the entire nested loop process.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of j at step 6?
A1
B2
C0
D3
💡 Hint
Check the 'j' column in execution_table row 6.
At which step does the outer loop condition become false?
AStep 8
BStep 9
CStep 4
DStep 3
💡 Hint
Look at the 'Condition i<2' column and find where it is false.
If we change the inner loop to run j < 2 instead of j < 3, how many total print outputs will there be?
A4
B6
C3
D2
💡 Hint
Multiply outer loop iterations (2) by new inner loop iterations (2).
Concept Snapshot
Nested loops run one loop inside another.
Outer loop runs once per each full inner loop run.
Inner loop resets each outer iteration.
Use nested loops to handle grids or pairs.
Syntax: for(...) { for(...) { ... } }
Full Transcript
Nested loops mean one loop runs inside another. The outer loop starts and for each value, the inner loop runs fully. For example, with i from 0 to 1 and j from 0 to 2, the program prints pairs of i and j. The inner loop completes all j values before i increases. Variables i and j change step by step as shown. The loops stop when i reaches 2. This helps when you want to repeat actions in a grid or pairs.