while loop javascript

JavaScript while loop can be used in many ways. The JavaScript while loop runs as long as your given condition is correct. The loop breaks after your given condition finishes. Let’s know how to use a while loop in JavaScript.

Syntax

while (your condition) {
  // statement executes
}

While Loop JavaScript

We have some excellent examples of javascript while loop, after seeing which you will find out how you can use javascript while loop.

<script type = "text/javascript">

	var value = 0;
 
	while (value < 10) {
	   document.write("Number : " + value + "<br>");
	   value++;
	}
 
</script>

Output:- The count will last till 15


<script type = "text/javascript">

	var value = 5;
 
	while (value < 15) {
	   document.write("Number : " + value + "<br>");
	   value++;
	}
 
</script>

Output:- 10 numbers will be printed from 5 to 14


<script type = "text/javascript">

	var value = 10;
 
	while (value < 15) {
	   document.write("Number : " + value + "<br>");
	   value++;
	}
 
</script>

Output:-  5 numbers will be printed from 10 to 14

While Loop Break Statement

According to the condition, this loop will run till 15, but if you want to break it, then you will have to make a separate condition for it. As you can see below, we have set 5 in the condition.

<script type = "text/javascript">

	var value = 0;
 
	while (value < 15) {
	   document.write("Number : " + value + "<br>");
	   value++;
	   if (value == 5) {
		  break;
		}
	}
 
</script>

Output: - 5 numbers will be printed from 0 to 4


/ ***** Checked if the value is above 5 ***** /
	  
<script type = "text/javascript">

	var value = 0;
 
	while (value < 15) {
	   document.write("Number : " + value + "<br>");
	   value++;
	   if (value > 5) {
		  break;
		}
	}
 
</script>

Output:- 6 numbers will be printed from 0 to 5

While Loop Continue Statement

If you want to remove a number from the middle, then for that you have to use the continue keyword.

<script>

	var i = 0;
	
	while (i < 10) {
		i++;
	if (i == 5) {
	  continue;
	}
	document.write("Number : " + i + "<br>");
}

</script>

Output:- Removed 5 out of between 1 and 10

Do While Loop

Syntax

do {
  // code statement
}
while (your condition);

The method of using Do While Loop is somewhat different. Inside it comes the first statement, then your condition. Let’s know how to use Do While Loop in JavaScript.

<script>

	var value = 0;
	
	do {
		document.write("Number : " + value + "<br>");
		value++;
	}
	while (value < 10);
  
</script>

Frequently Asked Questions

We have answers to some questions that people search on the Internet every day. Let’s know what those questions are and what their answers are.

Within which tag JavaScript is used?
JavaScript is used inside the script HTML tag

What is onmouseout?
It is used when you move your mouse over an element.

Where is insert a JavaScript inside HTML?
Inside head and body tag

How to link JavaScript file inside the HTML?
Using script src=”myfile.js”

What is onkeypress?
Onkeypress is an event which is used when the key is pressed.

How to JavaScript alert?
Using an alert function eg alert(“my alert”);

How to create a function in JavaScript?
Using “function DemoFunction()”

How to write while loop?
Using while (your condition)

How to use a JavaScript variable?
Using var demo;

Is JavaScript is a case sensitive language?
Yes JavaScript is a case sensitive language

What is onsubmit?
This is used to submit the form

Note:- All the examples you have seen inside this article have all been made by us and tested thoroughly. So you can use all these without any difficulty.

Related: 5 Javascript Replace Method you must know!