For Loop in JavaScript

By Hemanta Sundaray on 2022-05-01

A typical for loop includes an iterator variable, which is initialized, checked against the stopping condition and assigned a new value on each loop iteration.

A for loop contains three expressions separated by ; inside the parentheses:

  • An initialization starts the loop and can also be used to declare the iterator variable.
  • A stopping condition is the condition that the iterator variable is evaluated against – if the condition evaluates to true the code block will run, and if it evaluates to false the code will stop.
  • An iteration statement is used to update the iterator variable on each loop.

The for loop syntax looks like this:

For Loop

for (let counter = 0; counter < 5; counter++) {
  console.log(counter)
}

In this example, the output would be the following:

0
1
2
3
4

Join the Newsletter