Loop for in Javascript

JavaScript Loops: JavaScript loops is used to iterate the elements for a fixed number of times. if the number of the iteration is known and to iterate the piece of code using for, while, do while or for-in loops. It makes the code compact. It is mostly used in array. In JavaScript, you can use a for loop to iterate over a block of code multiple times.

There are also other types of loops in JavaScript, such as while and do-while, for, which offer different ways of expressing loops.

There are four types of loops in JavaScript.

for loop
while loop
do-while loop
for-in loop

1) JavaScript For loop

The iterates the elements for the fixed number of times. It should be used if number of iteration is known. The syntax of for loop is given below.

for (initialization; condition; increment)
{
code to be executed
}
Let’s see the simple example.

Output:

1
2
3
4
5

2) JavaScript while loop

The JavaScript while loop iterates the elements for the infinite number of times. It should be used if number of iteration is not known. The syntax of while loop is given below.

while (condition)
{
code to be executed
}
Let’s see the simple example.

Output:

11
12
13
14
15

3) JavaScript do while loop

The JavaScript do while loop iterates the elements for the infinite number of times like while loop. But, code is executed at least once whether condition is true or false. The syntax of do while loop is given below.

do{
code to be executed
}while (condition);
Let’s see the simple example.

Output:

21
22
23
24
25

4) JavaScript for in loop

it used to iterate the properties of an object.

Here’s a breakdown of the different parts:

Initialization: This is where you initialize a counter variable. It’s executed once before the loop starts.

Condition: This is a boolean expression that is evaluated before each iteration. If it evaluates to true, the loop continues; if it evaluates to false, the loop exits.

Iteration: This is where you can update the counter variable. It’s executed after each iteration of the loop.

Javascript training in hyderabad
contact

Enquiry Now
close slider
Scroll to Top
Call Now Button