Lesson 32

For Loops

Master counting loops

20 min
+25 XP
Step 1 of 5

What is a For Loop?

A for loop is perfect when you know exactly how many times you want to repeat something. It's like counting from 1 to 10!

For Loop Structure:

A for loop has three parts: initialization, condition, and update. All in one line!

for (let i = 0; i < 5; i++) {
  console.log("Count: " + i)
}
// Output: Count: 0, Count: 1, Count: 2, Count: 3, Count: 4

Breaking Down the For Loop:

1

Initialization: let i = 0

Sets up the counter variable (runs once at the start)

2

Condition: i < 5

Checks before each loop (if false, loop stops)

3

Update: i++

Runs after each loop iteration

Why use 'i'?

By tradition, programmers use 'i' as the counter variable (it stands for "index"). You can use any name, but 'i' is the most common!