JavaScript: For Loops

Example: While Loop in JavaScript


	<script>
	var x = 1;
	while (x < 5) { 
		document.write("<p>x = " + x + ".</p>");
		x++;
	}
	</script>
	

Output:

  x = 1.

  x = 2.

  x = 3.

  x = 4.

Run It


Example: Do ... While Loop in JavaScript


	<script>
	var x = 1;
	do 
	{
		document.write("<p>x = " + x + ".</p>");
		x++;
	}
	while (x < 5);
	</script>
	

Output:

  x = 1.

  x = 2.

  x = 3.

  x = 4.

Run It