Favorites

JavaScript for Statement

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee
for
JavaScript IterationThis statement is supported by all modern browsers.
Updated 28th November 2026

The for statement creates a loop with a starting step, a condition checked before each pass and an update step run after each pass.

Description

The for statement creates a loop with three optional parts in parentheses: an initialization run once at the start (usually declaring a counter), a condition checked before every iteration, and an afterthought run after every iteration (usually incrementing the counter). The loop continues while the condition is truthy.

Declare the counter with let, which gives each iteration its own copy, so callbacks created in the loop see the right value. To loop over the items of an array or other iterable, for...of is simpler; the classic for loop is best when you need the index or a custom step.

Any of the three parts can be left out. Without a condition, the loop runs until a break or return; for (;;) is an infinite loop. You can also declare several variables in the initialization and update several in the afterthought using the comma operator. Changing the array you're looping over inside a classic for loop, for example removing elements, can make you skip items, so loop backwards in that case.

Syntax

for (initialization; condition; afterthought) {
  statements
}

Example

for (let i = 0; i < 3; i++) {
  console.log('Pass', i);
}

const items = ['a', 'b', 'c', 'd'];
for (let i = items.length - 1; i >= 0; i -= 2) {
  console.log(i, items[i]);
}

Browser Support

The following information will show you the current browser support for the for statement. Hover over a browser icon to see the version that first introduced support for this statement.

This statement is supported by all modern browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported
☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee