LESSON 24 OF 50

Count with for

Group initialization, condition and update in one loop.

Official sourceECMAScript — for statementReviewed: 2026-08-26
STEP 1 · LEARN

How it works

The classic for form has three parts: initialization runs once, the condition is checked before each iteration, and the update runs after the body.

Correct example
for (let index = 1; index <= 3; index += 1) {
  console.log(index);
}

index starts at 1 and is printed through 3 inclusive.

Common mistake
for (let index = 1; index < 3; index += 1) { console.log(index); }

The < operator stops the loop before index reaches 3.

STEP 2 · APPLY

Your exercise

Use for to print 1, 2 and 3 through the index variable.

STEP 3 · RUN

Write and run the code

script.jsJAVASCRIPT
Isolated consoleisolated
Preparing the runtime…
Run the code to see the result.
The draft stays only in this browser.Isolated QuickJS · no DOM, session or network
STEP 4 · CHECK

What needs to pass

0%not checked
  • for declares index from 1, checks <= 3, updates and prints it.
  • The output is 1, 2, 3.
  • The loop finishes without an error.
Explain the solution logic

The three header parts control the loop start, limit and step.