JavaScript

5 Type of JavaScript Loops – The Ultimate Guide

Share your learning

Today we are going to learn 5 type of JavaScript loops in detail with a complete tutorial and examples.

The simplest meaning of loop is to repeat something.

“A structure, series, or process, the end of which is connected to the beginning.”

In the programming term when we need to repeat a set of code statements for a specific time, we can use loops.  

In the JavaScript we have the following loop concepts available.

  1. For loop
  2. Do while
  3. While
  4. For in
  5. For of

You might be interested in jQuery clone a form elements with unique attributes.

JavaScript For Loop

JavaScript for loop is a widely used looping statement. It is very easy to use as given in the example below.

Syntax

Loop initiator, condition and increment/decrement statement all will be in the same line.

for (initiate Loop; Loop Condition; increment/decrement Statement) {
   // loop body
}

For loop example

var users = [
  'raj', 'neha', 'mukund', 'ravi', 'devid'
];

for( let i = 0; i < users.length; i++ ) {
  console.log('Hello - ' + users[i]);
}

Output

Console :

"Hello - raj"

"Hello - neha"

"Hello - mukund"

"Hello - ravi"

"Hello - devid"

JavaScript Do while loop

JavaScript do while loop is a looping statement which can be used if we want to run the loop at least once and then need to check the condition to allow for the next loop.

As its name suggests, do something while the condition is true. So, this loop executes its purpose at once and then test the condition in the while statement, if that is true then the loop will execute again otherwise control will pass to the next statement.

Syntax

do {
  // loop body
} while ( loop condition );

Do while loop example

let j = 0;

do {
  console.log('Hello - ' + users[j]);
  j++;
} while ( j < users.length );

Output

Console :

"Hello - raj"

"Hello - neha"

"Hello - mukund"

"Hello - ravi"

"Hello - devid"

JavaScript While Loop

JavaScript while loop is similar to for loop, the only difference is loop initiator and increment/decrement statement.

Syntax

We have to declare our initiator statement first and then the condition will pass to the while loop. Next, in the while loop body we need to define our increment/decrement statement.

//initiate loop;

while ( loop condition ) {
   //loop body

  //increment/decrement statement
}

While loop example

let k = 0;

while ( k < users.length ) {
  console.log('Hello - ' + users[k]);
  k++;
}

Output

Console :

"Hello - raj"

"Hello - neha"

"Hello - mukund"

"Hello - ravi"

"Hello - devid"

JavaScript For In

JavaScript for in is another looping statement which is very interesting. Because here we don’t need loop initiator and increment decrement statement.

It will get the all indexes one by one from the array and terminate itself at the end of the array elements.

Syntax

We just need a condition for this loop that is to execute the following statements until we have an element remaining in the array.

for ( index in array ) {
  // loop body
}

For In loop example

for( index in users ) {
   console.log("Hello - " + users[index]);
}

Output

Console :

"Hello - raj"

"Hello - neha"

"Hello - mukund"

"Hello - ravi"

"Hello - devid"

JavaScript for of loop

JavaScript for of is similar to for in loop the major difference is that for in loop retrieve the index and for of loop retrieve the element value. 

It will break the loop when all elements of the array or object will be iterated.

Syntax

Again we just need a condition for this loop that is to execute the following statements until we have an element remaining in the array as in the JavaScript for in loop.

for ( element of array ) {
  // loop body
}

For of loop example

for( user of users ) {
   console.log("Hello - " + user);
}

Output

Console :

"Hello - raj"

"Hello - neha"

"Hello - mukund"

"Hello - ravi"

"Hello - devid"

Try Javascript loops on Codepen

Go To Sbsharma codepen

Open the console to see the output on codepen.

Hope you find this article useful.

See you in the next learning journey.

Satpal

Recent Posts

How to Switch PHP Versions in XAMPP Easily: Managing Multiple PHP Versions on Ubuntu

Today we are going to learn about managing multiple PHP versions on ubuntu with xampp.…

1 year ago

How to Use Coding to Improve Your Website’s SEO Ranking?

Let's understand about how to use coding to improve your website's SEO. In today’s computerized…

1 year ago

Most Important Linux Commands for Web Developers

Let's understand the most important linux commands for web developers. Linux, as an open-source and…

1 year ago

Top 75+ Laravel Interview Questions Asked by Top MNCs

Today we are going to discuss top 75+ Laravel interview questions asked by top MNCs.Laravel,…

1 year ago

Mailtrap Integration for Email Testing with Laravel 10

Today we will discuss about the Mailtrap integration with laravel 10 .Sending and receiving emails…

1 year ago

Firebase Cloud Messaging (FCM) with Ionic 6: Push Notifications

Today we are going to integrate FCM (Firebase Cloud Messaging) push notifications with ionic application.Firebase…

1 year ago