top of page

For loop in JavaScript

In programming languages a loop is a structure that contains a condition and one or more statements which is repeatedly executed while the condition is met. In a loop, each single examination of the condition and statements is called an iteration. The loop continue as long as the condition is met.When the condition is no longer met, the flow continue to the next statement following the loop structure.

​

Here we will look at the for loop, which has this syntax:

Initializer: a variable that will be used as a counter, to count the number of loop iterations. It's normal to name this variable i.

​

Condition: this is an expression that is for boolean true. When found true the loop will then execute the code inside the loop.

​

Modifier: a statement that modifies a value used in the condition. The purpose is to at some time make the condition return false and end the loop.

​

The code for running a for loop 20 times can look like this:

Here the counter variable named i is incremented on each iteration until it reach the value 20. Then the condition will be evaluated to be false and the loop will end.

bottom of page