JavaScript Loops

by anupmaurya
0 comment
JavaScript Loops

In this article you’ll learn about JavaScript Loops. Loops can execute a block of code a number of times.

What is Loops ?

If you want to run the same code over and over again, each time with a different value and loops are the super power which helps us to do that.

For example, suppose we want to print “Hello Stars!!” 10 times. This can be done in two ways,

console.log("Hello Stars!!"); console.log("Hello Stars!!"); console.log("Hello Stars!!"); console.log("Hello Stars!!"); console.log("Hello Stars!!"); console.log("Hello Stars!!"); console.log("Hello Stars!!"); console.log("Hello Stars!!"); console.log("Hello Stars!!"); console.log("Hello Stars!!");
Code language: JavaScript (javascript)

let’s break down above code using For Loop

var i; for (i = 0; i < 10; i++) { console.log("Hello Stars!!\n"); }
Code language: JavaScript (javascript)

Now, you all got it how loops are quiet useful. Next, what are the different kinds of Loops.

Different Kinds of Loops

JavaScript supports different kinds of loops

  • for – loops through a block of code a number of times.
  • for/in – loops through the properties of an object.
  • for/of – loops through the values of an iterable object.
  • 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.

Let’s discuss about each of them on by one,

1.For loop

For Loop in JavaScript is used when number of iteration is known, it iterates the elements for the fixed number of times.

//Syntax of For Loop
for (initialization; condition; increment)  
{  
    code to be executed  
}  

Let understand with an example,

for (let i = 0; i < 5; i++) { console.log(i); //0 1 2 3 4 }
Code language: JavaScript (javascript)

In above code , we are iteration the different values of i until i is less than 5 .

2. For In loop

For In Loop in JavaScript is used when we have to iterate loops through the properties of an Object.

//Syntax of For In Loop
for (key in object) {
  // code block to be executed
} 

Let understand with an example,

const student = { firstStudent:"Ravi Prakash", secondStudent:"Laxami Rawat", thirsStudent:"Anup Maurya", }; let text = ""; for (let x in student) { console.log(student[x]); }
Code language: JavaScript (javascript)

In the above code, we are iterating over the object of student and printing the names of student.

3. For Of Loop

For Of Loop in JavaScript is used when we have to iterate through the values of an iterable object.

//Syntax of For Of Loop
for (variable of iterable) {
  // code block to be executed
}

Let understand with an example,

const names= ["Rakesh", "Anup", "Rashmi"]; let text = ""; for (let x of names) { console.log(x); // Rakesh Anup Rashmi }
Code language: JavaScript (javascript)

In the above code, we are iterating over the values of names object.

4. While Loop

while loop is used , when we have to loops through a block of code as long as a specified condition is true. It is also called as exit-control loop.

Syntax of while Loop
while (condition) {
  // code block to be executed
}

Let understand with an example

while (i < 10) { console.log("I write Code"); }
Code language: JavaScript (javascript)

In In the above code, we are printing I write Code again and again until i is less than 10 .

5. Do While Loop

do while loop is used, when we have to execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true. It is also called as entry-control loop.

//Syntax of do while Loop
do {
  // code block to be executed
}
while (condition);

Let understand with an example

do { console.log("I write Code"); } while (i < 10);
Code language: JavaScript (javascript)

In In the above code, we are print I write Code atleast once and after which it will keep on printing until i is less than 10 .

That’s all for this article and with that, it’s a wrap! I hope you found the article useful. Thank you for reading, If you have reached so far, please like the article, It will encourage me to write more such articles. Do share your valuable suggestions, I appreciate your honest feedback!

Ta da! Having fun learning JavaScript!! 

You may also like