Jobs
Blogs
Courses
Mentorship
Partners
For Employers
e2e logo

JavaScript Loops

mac

JavaScript Loops

The JavaScript loops are used to iterate the piece of code using for, while, do while of for-in loops. It makes the code compact and mostly used in arrays.

Following are the different types of loops in JavaScript

1. for loop

2. while loop and

3. do-while loop 

1. JavaScript For Loop

The JavaScript for loop iterates the elements for the fixed numbers of times. It should be used if the number of iteration is known.

Following is the syntax

For example

2. JavaScript While loop

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

The syntax of while loop is 

For example

 3. JavaScript Do While Loop

The do while loop is the variant of the while loop. This loop will execute the code block once, before the checking the if condition is true, then it will repeat the loop as long as the condition is true.

The syntax of do while loop is

Following is the example

The example above uses the do while loop. The do while loop will always be executed at least once, even if the condition is false, because the code block will executed before the condition is tested.
And don't forget to increase the variable used in the condition, otherwise the loop will never end.

Conclusion
In this blog covered JavaScript loops.