while

Loops through the statements enclosed within theĀ { code block } until the test_expression is false:

 

while (test_expression) {
statements ;
}

 

<html>
	<head>
		<title>JavaScript</title>
	</head>
	<body>
	<script type="text/javascript">
		var val = 0;

		while (val < 5) {

			document.write("val = " + val + "<br>");

			val++ ; //increment val
		}
	</script>
	</body>
</html>

Save & refresh browser:

val = 0
val = 1
val = 2
val = 3
val = 4

Leave a Reply