Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

for — loop with initialization, condition, and update

The for keyword creates a loop. Its classic form combines initialization, a condition checked before each iteration, and an update performed after the loop body.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

for (initialization; condition; update) {
  statements
}

for is a reserved JavaScript keyword: it cannot be a variable name. The classic loop runs initialization once, then checks condition. If the condition converts to true, it executes the body, then update, and checks again. The body may never execute.

Any of the three components can be omitted, but both semicolons remain mandatory. Omitting the condition removes conditional termination: for (;;) {} does not stop on its own. break exits the loop, while continue targeting this loop skips the remaining body but still runs update before the next check.

A variable declared with let in the header is scoped to the loop and has a separate binding for each iteration; var does not provide that binding. Use braces and watch for an accidental semicolon after ), which creates an empty body.

Do not confuse this form with for...in, which enumerates enumerable string property keys, including inherited ones, or for...of, which retrieves values from an iterable. The example sums the numbers from 1 through 3.

Examples

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

Result

6

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources