Loops are used to execute a block of code
a specified number of times.
In JavaScript, there are many loops which can be used like JavaScript for
loop, while
, do while
or for-in
loops.
Loops | Description |
---|---|
for | loops through a block of code a specified number of times. |
for in | The JavaScript for/in statement loops through the properties of an object. |
while | loops through a block of code as long as the specified condition is true. |
do while | loops through a block of code once, and then repeats the loop as long as the specified condition is true. |
The for
loop iterates the block of code for a fixed number of times. It is used when the number of iteration is known. The syntax of for loop is given below.
The parameters of the for loop statement have following meanings:
Here in this example, firstly 'i'
is initialized a value, then a condition is given to control the loop and then the increment
operator is used to increment the value of 'i'. In this loop every value of 'i' will get printed on the screen till the given condition is true.
Here in the example below, an array storing 5 values is iterated by the help of for
loop. The index value of the array is incremented with every loop and then one by one all the values of the array gets printed on the screen. Take a look in try-it editor to understand the working of for
loop in arrays.
Follow Us: