Loading...
Loading...
Loops inside loops. Process grids, matrices, and multi-dimensional data.
Programs make decisions and repeat work. Conditionals choose paths, loops repeat actions, and nested structures combine both. Mastering control flow turns a script that runs once into software that reacts to data and user input.
Learn Nested Loops in JavaScript — covering How It Works, Grid Visualization, Iteration Counting, Common Patterns with interactive visual examples and step-by-step explanations.
Control flow is how programs decide what to do next. If a user is logged in, show the dashboard; otherwise, redirect to login. If a cart total exceeds a threshold, apply a discount. These everyday behaviors all depend on conditionals and loops.
This module walks through if/else, switch, for and while loops, and how to combine them without losing readability. Visual step-throughs help you see exactly which branch runs and how loop counters change on each iteration.
Understanding nested loops is essential for every JavaScript developer. It shows up in frontend UI code, backend APIs, and framework internals — and getting it wrong leads to bugs that are hard to trace. This chapter builds intuition with visuals so the behavior sticks.
The inner loop completes all its iterations for each single iteration of the outer loop.
// Outer loop runs 3 times for (let i = 0; i < 3; i++) { // Inner loop runs 3 times FOR EACH outer iteration for (let j = 0; j < 3; j++) { console.log(i, j); } } // Total: 3 × 3 = 9 pairs
🕐 Think of it like a clock:
Outer loop (i) = hours hand → moves slowly
Inner loop (j) = minutes hand → completes full rotation for each hour
For each step of the outer loop, the inner loop runs completely from start to finish.
i=0: j=0, j=1, j=2 (inner completes) i=1: j=0, j=1, j=2 (inner completes again) i=2: j=0, j=1, j=2 (inner completes again)
The outer loop advances only after the inner loop finishes all its iterations.
Watch nested loops fill a grid cell by cell.
for (let row = 0; row < 4; row++) { for (let col = 0; col < 4; col++) { // Process cell at (row, col) } }
row = 0, col = 0
Step 1 of 16
Total iterations = outer × inner.
Total iterations:
3×4=12
// 3 levels deep: for (let i = 0; i < 3; i++) { // 3 times for (let j = 0; j < 4; j++) { // × 4 times // Total: 3 × 4 = 12 operations } } // With 3 nested loops: for (let i = 0; i < 10; i++) { // 10 for (let j = 0; j < 10; j++) { // × 10 for (let k = 0; k < 10; k++) { // × 10 // = 1,000 total operations! } } }
⚠️ Nested loops multiply. 3 loops of 100 = 100 × 100 × 100 = 1,000,000 operations. Be careful with deep nesting!
Real-world uses of nested loops.
const matrix = [ [1, 2, 3], [4, 5, 6], [7, 8, 9], ]; for (let row = 0; row < matrix.length; row++) { for (let col = 0; col < matrix[row].length; col++) { console.log(matrix[row][col]); } } // 1, 2, 3, 4, 5, 6, 7, 8, 9
Nested loops can be expensive. Know when to avoid them.
| Loops | Complexity | n=1000 |
|---|---|---|
| 1 loop | O(n) | 1,000 |
| 2 nested | O(n²) | 1,000,000 |
| 3 nested | O(n³) | 1,000,000,000 |
✓ When nested loops are fine:
✗ When to avoid:
// ✗ O(n²) — checking duplicates with nested loop for (let i = 0; i < arr.length; i++) { for (let j = i + 1; j < arr.length; j++) { if (arr[i] === arr[j]) found = true; } } // ✓ O(n) — use a Set instead const seen = new Set(); for (const item of arr) { if (seen.has(item)) { found = true; break; } seen.add(item); }
Common questions about nested loops.