Loops In JavaScript

Loops In JavaScript

A loop is a sequence of instructions, repeated until a certain condition is reached. The main concept behind the loop is that it repeats the task again and again in the program to reduce the time and effort.

Types of loop

1. Entry controlled loops

In entry controlled loops “Test condition is checked first, then the body of the loop is executed".

2. Exit controlled loops

In entry controlled loops “Body of loop is executed first, and then the condition is tested”.

while, do-while, for, for-in and for-of are the types of loops in JavaScript. In which do-while is the exit controlled loop. Now, the question is why it is exit controlled loop, Don’t worry we’ll discuss it.


While loop :

while loop executes the block of code until the given condition evaluates to true. As soon as the condition fails, the loop is stopped. The syntax of the while loops is:-

Syntax is while ( condition ) { //body of the loop }

Example of while loop:-

let i=1;
while(i < 6) {   
    console.log(i);   
    i++; 
}

//output:
1
2
3
4
5

In the given example, the body of the loop is executed until the value of i is less than 6 and the value of i increases by 1 when the body of the loop is executed once.


Do-while loop :

In a do-while loop, the body of the loop is executed before the condition is checked. The condition is checked at the end of each iteration of the loop.

The do-while loop is closely related to the while loop, the only difference is that body of the loop is executed first then after checks for the condition i.e.(exit controlled loop).

Syntax is do{ //body of loop }while( condition )

Example of the do-while loop:-

let i=1;
do{    
   console.log(i);
   i++; 
}while(i < 6);

//output:
1
2
3
4
5

In the given example, the body of the loop prints the value of i and increase by 1 when the body of the loop is executed once and at the end condition is checked i.e. i<6


For loop :-

For loop repeats the block of code until a certain condition is reached and is used to repeat the block of code a certain number of times.

Syntax is for(initialization; condition; increment){ //body of loop }

In syntax, initialization is used to initialize the variable once before the first execution of the body of the loop, the condition is evaluated every time at the beginning of each iteration, increment updates the variable at each iteration of the loop.

Example of for loop:-

for(let i=1; i<6; i++){
    console.log(i); 
}

//output:
1
2
3
4
5

For/In loop :-

The For/In statement loops the properties of an object. In each iteration, one property of the object is assigned to the variable.

Syntax is for(variable in object){ //body of loop }

The variable in for/in loop is a string, not a number.

let myObj = { 
   a:”apple”,
   b:”ball”,
   c:”cat”,
   d:”dog” 
};

for(prop in myObj){
   console.log(myObj[prop]); 
}

//output:
apple
ball
cat
dog

For/Of loop :-

The for/of loop is introduced in ES6. The for/of statement loops over the objects or data structures that are iterable, such as an array, string, etc. The for/of loop does not work on objects because they are not iterable.

Syntax is for( variable of iterable){ //body of loop }

Example:-

let arr = [“apple”, “ball”, “cat”];
for( variable of arr){
   console.log(variable);
}

//output:
apple
ball
cat