Loops in Javascript. How to use loops in JS? – Learn Javascript Online!

Hey, guys, I am Rahul and you are reading a Javascript learning session post by me. Hopefully, till now you guys have learned well. Am I right? Oh saying yes! Thanks…

I am providing you content with detailed clarification as much as I can. So let’s start with today session that is on While loop in Javascript.

Okay, first of all, while is a loop. And do you know what is a loop?

Its like a circulation motion in a ring form. start and end point are same. Means, when you start, the same start become end point, and when you end the same point become start. That’s it!

 

Types of Loops in Javascript

  1. for
  2. while
  3. do while
  4. for in loop

Now I am going to describe all the above loops in the below post. So get ready to learn them.

What is FOR loop in Javascript?

for loop is a type of loop in the Javascript programming. This is the loop which we see most commonly used in the programs. And now the question is that when to use and how?

For loop should be used only when we know the condition range. Means, when we know the value for which time loop is going to be executed.

For Loop Example in Javascript by Rahul Rahi

Syntax

for (initializations; condition; increment)  { 

statements to be executed.

}

Are you searching for example? Please check it below:

FOR loop example in Javascript by Rahul Rahi

Here the code goes:

<html>
<body>
<script> 
for (i=1; i<=5; i++) //for loop is used till 5 numbers and incremented using i++.
{ 
document.write(i + "<br/>") //here writing the value of i variable and giving a line break after each write using br tag.
} 
</script> 
</body>
</html>

Secondly, we have while loop. Let’s get into it.

What is While loop in Javascript?

While loop is a loop which iterates or utterly repeat the execution number of times. It should be included in the program if the condition is not known.

while loop example in javascript by rahul rahi codingparks.com

Syntax

while (condition)  {  

code to be executed 

 }

while loop example in javascript by rahul rahi codingparks.com

Code is here dear

<html>
<body>
<script> 
var i=1; //decleration of i variable with static value or fixed value.
while (i<=10) //condition is given till 10 or equal to 10 mind <= sign < will goto 9 and <= will goto 10. 
{ 
 document.write(i + "<br/>"); //printing value of 1 with next line.
 i++; //increasing the value of variable i

} 
</script> 
</body>
</html>

What is “do while” loop in Javascript?

Well, do while loop is same as the while but in this statements are executed at least once whether the condition is true or false. In the other manner, it is same as while.

dowhileloop

Syntax

do{

Statement to be executed

} do while(conditions)
Do While loop example in javascript by Rahul Rahi Codingparks

Code is here

<html>
<body>
<script> 
var i=3; 
do{ 
document.write(i + "<br/>"); 
i++; 
}while (i<=5); 
</script> 
Codingparks.com 
</body>
</html>

What is for in loop in javascript?

For in loop is used to loop through the object properties. In later session I will explain you this.

Syntax

for (variablename in object){
   statement or block to execute
}

for-in-loop-example-in-javascript-by-rahul-rahi

Code is here

<html>
 <body>
 
 <script type="text/javascript">
 
 var aProperty;
 document.write("Navigator Object Properties<br /> ");
 
 for (aProperty in navigator) {
 document.write(aProperty);
 document.write("<br />");
 }
 document.write ("Exiting from the loop!");
 </script>
 
 </body>
</html>

This ends here and stay updated for next session. Like us on Facebook and twitter to stay updated.

 

Leave a Reply

CONTACT ME