Loops in JavaScript – Tutorialpath

Loop in JavaScript – Welcome to all our readers on our website at www.tutorialpath.com. As you all know that here on our website we will provide you lots of information related to the latest and newly introduced technology which helps you to get updated with the latest tech. Here you will get all the useful information which is required in this tech world.

As you know technology is one of the trending parts of this world. So, here again, we came up with the latest and new tech information which is related to the process of creating Loop in JavaScript. With the help of this article, you will get to know the complete process of how to create JavaScript if-else Statement in a step by step manner. So, simply have a look at this article and grab all the useful information which is going to be very helpful for you.

When writing a program, you may encounter a situation where you need to perform an action repeatedly. In such situations, you have to write a loop statement to reduce the number of lines.

For Loop, While Loop and Do While Loop in JavaScript

In this tutorial, we will learn-

  • How to use Loop?
  • Different Types of Loops
  • for loop
  • while loop
  • do…while loop

How to use Loop?

Loops are useful when you have to repeatedly execute the same lines of code until a specific condition is correct or for a particular situation. Suppose you want to type the message ‘Hello’ 100 times on your webpage. Of course, you have to copy and paste the same line 100 times. Instead, if you use a loop, you can complete this task in only 3 or 4 lines.

Types of Loops

There are mainly four types of loops in JavaScript.

  1. For Loop
  2. While Loop
  3. Do…while Loop

For Loop:- 

Statement 1 is executed even before the looping code is executed. So, this statement is typically used to specify the values ​​of the variable that will be used inside the loop.

Statement 2 is the state of executing the loop.

Statement 3 is executed every time the looping code is executed.

Syntax:

    for(statement1; statement2; statment3)
      {

         lines of code to be executed

      }

Example:

<html>
<head>
<script type=”text/javascript”>
var students = new Array(“Rajan”, “Mukesh”, “Arun”, “Tutorialpath”, “Parveen”);
document.write(“<b>Using for loops </b><br />”);
for (i=0;i<students.length;i++)
{
document.write(students[i] + “<br />”);
}
</script>
</head>
<body>
</body>
</html>

Output:

Using for loops
Rajan
Mukesh
Arun
Tutorialpath
Parveen

While Loop:

JavaScript includes a loop for executing the code, as long as it does not meet a specified condition. Unlike loops, while loops only require position expressions.

Syntax:

   while (expression) {
      Statement(s) to be executed if expression is true
   }

Example:

<html>
<head>
<script type=”text/javascript”>
document.write(“<b>Using while loops </b><br />”);
var i = 0, j = 1, k;
document.write(“Fibonacci series less than 40<br />”);
while(i<40)
{
document.write(i + “<br />”);
k = i+j;
i = j;
j = k;
}
</script>
</head>
<body>
</body>
</html>

Output:

Using while loops
Fibonacci series less than 40
0
1
1
2
3
5
8
13
21
34

Do…while Loop:

JavaScript includes another flavor of loops, which is doo-loop. While loop-to-loop is the same, while loop is the only difference, it evaluates the expression of a position after the execution of a code block. So while loop will execute the code block at least once.

Syntax:

do

{

block of code to be executed

} while (condition)

Example:

<!DOCTYPE html>

<html>

 

<head>

<title>

JavaScript  loop

</title>

</head>

 

<body style=”text-align:center;”>

 

<div>

<h1>Tutorialpath</h1>

<h2>JavaScript  Loop</h2>

</div>

 

<p id=”GFG”></p>

 

<!– Script to use do-while loop –>

<script>

var print = “”

var val = 0;

 

do {

print += “Tutorialpath” + val;

print +=”<br>”;

val+=1;

}

while (val < 6);

 

document.getElementById(“GFG”).innerHTML

= print;

</script>

</body>

 

</html>

Output:

Tutorialpath

JavaScript Loop

Tutorialpath 0
Tutorialpath 1
Tutorialpath 2
Tutorialpath 3
Tutorialpath 4
Tutorialpath 5

Difference between while and do-while loop:

The do-loop executes the contents of the loop once before checking the state of the loop. While the while loop will check the status before executing the contents.

While Loop Do- While Loop
It is an entry condition looping structure. It is an exit condition looping structure.
The block control condition is available at the starting point of the loop. The block control condition is available at the endpoint of the loop.
The number of iterations depends on the condition mentioned in the while block. Irrespective of the condition mentioned in the do-while block, there will a minimum of 1 iteration.

Conclusion

In this tutorial, we learned about the For loop, While Loop and do… While loop, and infinite loops in JavaScript.

Leave a Reply

Your email address will not be published. Required fields are marked *