---
description: Loops are useful when you have to execute the same lines of code repeatedly, for a specific number of times or as long as a specific condition is true. Suppose you want to type a &lsquo;Hello&rsquo;
title: For, While and Do While LOOP in JavaScript (with Example)
image: https://www.guru99.com/images/how-to-use-loops-in-javascript.png
---

 

[Skip to content](#main) 

## How to use Loop?

Loops are useful when you have to execute the same lines of code repeatedly, for a specific number of times or as long as a specific condition is true. Suppose you want to type a ‘Hello’ message 100 times in your webpage. Of course, you will have to copy and paste the same line 100 times. Instead, if you use loops, you can complete this task in just 3 or 4 lines.

[![Use Loops in Javascript](https://www.guru99.com/images/JavaScript/javascript4_1.png)](https://www.guru99.com/images/JavaScript/javascript4%5F1.png)

### Different Types of Loops

There are mainly four types of loops in [JavaScript](https://www.guru99.com/introduction-to-javascript.html).

1. for loop
2. for/in a loop (explained later)
3. while loop
4. do…while loop

## for loop

Syntax:

for(statement1; statement2; statment3)
{
lines of code to be executed
}

1. The statement1 is executed first even before executing the looping code. So, this statement is normally used to assign values to variables that will be used inside the loop.
2. The statement2 is the condition to execute the loop.
3. The statement3 is executed every time after the looping code is executed.

Try this yourself:

<html>
<head>
	<script type="text/javascript">
		var students = new Array("John", "Ann", "Aaron", "Edwin", "Elizabeth");
		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>

## while loop

Syntax:

while(condition)
{
lines of code to be executed
}

The “while loop” is executed as long as the specified condition is true. Inside the while loop, you should include the statement that will end the loop at some point of time. Otherwise, your loop will never end and your browser may crash.

Try this yourself:

<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>

## do…while loop

Syntax:

do
{
block of code to be executed
} while (condition)

The do…while loop is very similar to while loop. The only difference is that in do…while loop, the block of code gets executed once even before checking the condition.

Try this yourself:

<html>
<head>
	<script type="text/javascript">
		document.write("<b>Using do...while loops </b><br />");
		var i = 2;
		document.write("Even numbers less than 20<br />");
		do
		{
			document.write(i + "<br />");
			i = i + 2;
		}while(i<20)
	</script>
</head>
<body>
</body>
</html>

This code is editable. Click Run to Execute  

#### Summarize this post with:

ChatGPT Perplexity Grok Google AI 

**Stay Updated on AI** **Get Weekly AI Skills, Trends, Actionable Advice.** 

##### Sign up for the newsletter

Subscribe for Free 

You have successfully subscribed.  
Please check your inbox. 

![AI-Newsletter]() Chosen by over **350,000+** professionals 

[Scroll to top ](#wrapper)Scroll to top 

× 

Toggle Menu Close 

Search for: 

Search

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://www.guru99.com/#organization","name":"Guru99","sameAs":["https://www.facebook.com/Guru99Official","https://twitter.com/guru99com"],"logo":{"@type":"ImageObject","@id":"https://www.guru99.com/#logo","url":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","contentUrl":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","caption":"Guru99","inLanguage":"en-US"}},{"@type":"WebSite","@id":"https://www.guru99.com/#website","url":"https://www.guru99.com","name":"Guru99","publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US"},{"@type":"ImageObject","@id":"https://www.guru99.com/images/how-to-use-loops-in-javascript.png","url":"https://www.guru99.com/images/how-to-use-loops-in-javascript.png","width":"509","height":"431","inLanguage":"en-US"},{"@type":"BreadcrumbList","@id":"https://www.guru99.com/how-to-use-loops-in-javascript.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":"1","item":{"@id":"https://www.guru99.com","name":"Home"}},{"@type":"ListItem","position":"2","item":{"@id":"https://www.guru99.com/javascript","name":"JavaScript"}},{"@type":"ListItem","position":"3","item":{"@id":"https://www.guru99.com/how-to-use-loops-in-javascript.html","name":"For, While and Do While LOOP in JavaScript (with Example)"}}]},{"@type":"WebPage","@id":"https://www.guru99.com/how-to-use-loops-in-javascript.html#webpage","url":"https://www.guru99.com/how-to-use-loops-in-javascript.html","name":"For, While and Do While LOOP in JavaScript (with Example)","dateModified":"2024-03-09T15:00:01+05:30","isPartOf":{"@id":"https://www.guru99.com/#website"},"primaryImageOfPage":{"@id":"https://www.guru99.com/images/how-to-use-loops-in-javascript.png"},"inLanguage":"en-US","breadcrumb":{"@id":"https://www.guru99.com/how-to-use-loops-in-javascript.html#breadcrumb"}},{"@type":"Person","@id":"https://www.guru99.com/author/james","name":"James Hartman","description":"I am James Hartman, a seasoned professional in Oracle Certified Java Professional tutorials, specializing in crafting comprehensive guides to help you excel in your Java certification journey.","url":"https://www.guru99.com/author/james","image":{"@type":"ImageObject","@id":"https://www.guru99.com/images/james-hartman-author-v2-120x120.png","url":"https://www.guru99.com/images/james-hartman-author-v2-120x120.png","caption":"James Hartman","inLanguage":"en-US"},"worksFor":{"@id":"https://www.guru99.com/#organization"}},{"@type":"NewsArticle","headline":"For, While and Do While LOOP in JavaScript (with Example)","keywords":"javascript, java","dateModified":"2024-03-09T15:00:01+05:30","articleSection":"JavaScript","author":{"@id":"https://www.guru99.com/author/james","name":"James Hartman"},"publisher":{"@id":"https://www.guru99.com/#organization"},"description":"Loops are useful when you have to execute the same lines of code repeatedly, for a specific number of times or as long as a specific condition is true. Suppose you want to type a &lsquo;Hello&rsquo;","copyrightYear":"2024","copyrightHolder":{"@id":"https://www.guru99.com/#organization"},"name":"For, While and Do While LOOP in JavaScript (with Example)","@id":"https://www.guru99.com/how-to-use-loops-in-javascript.html#richSnippet","isPartOf":{"@id":"https://www.guru99.com/how-to-use-loops-in-javascript.html#webpage"},"image":{"@id":"https://www.guru99.com/images/how-to-use-loops-in-javascript.png"},"inLanguage":"en-US","mainEntityOfPage":{"@id":"https://www.guru99.com/how-to-use-loops-in-javascript.html#webpage"}}]}
```
