Hi Guys,
If you want to run the same code over and over again, each time with a different value. Loops can execute a block of code a number of times.
There are mainly four types of loops in JavaScript.
- for : loops through a block of code a number of times
- while : loops through a block of code while a specified condition is true
- do/while : also loops through a block of code while a specified condition is true
Loops are useful when we have to execute the same lines of code repeatedly. For a specific number of times or as long as a specific condition is true
for loop
Syntax
for(statement1; statement2; statment3) { lines of code to be executed }
Detail Description
- statement1: It is executed first even before executing the looping code. So, this statement is normally used to assign values to variables that will be used inside the loop.
- statement2: It is the condition to execute the loop.
- statement3:It is executed every time after the looping code is executed.
Javascript Loop!!!
while loop
The “while loop” is executed as long as the specified condition is true.
Syntax
while(condition) { lines of code to be executed }
do…while loop
The do…while loop is very similar to while loop.
Syntax
do { block of code to be executed } while (condition)
If anyone has doubts on this topic then please do let me know by leaving comments or send me an email.