Core JavaScript

Intermediate JavaScript

JavaScript Reference


JavaScript DO WHILE Loop


JavaScript do...while loop check condition and execute a block of code for a specify number of times, until the condition become false.

JavaScript do...while block of statements executed before evaluated the condition. At least one time block of statements executes if the condition doesn't satisfy.

Syntax :

do {
	statements; 	// Do stuff if while true
	increment;		// Update condition variable value
} while (condition)
do {		
	...
	...
} while (true)		// condition evaluates to true
do {
	...
	...
} while (1) 		// Condition always true

Example : Following do...while loop example execute block of code until number value become 10. When number value become 11 the condition no longer satisfy and finally skip to execute do...while block.

<script>
    number = 1;
    do {
        document.writeln(number + " times");
        number++;
    } while (number <= 10) 
    document.writeln("Total " + (number - 1) + " times loop repeat");
</script>

Run it...   »

Example Result :


Array Loops in JavaScript

do...While loop to go through array,

JavaScript array.length property indicates length of a array.

<script>
    var arr = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100];
    number = 0;
    do {
        document.writeln("Value of arr[" + number + "] : " + arr[number]);
        number++;
    } while (number < arr.length)
    document.writeln("End of Loop.");
</script>

Run it...   »

Example Result :


Reverse Array Loop in JavaScript

Following example display all array elements value in reverse order.

<script>
	var arr = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100];	
    number = arr.length - 1;			// array index start from 0
	do{
	    document.writeln("Value of arr[" + number + "] : " + arr[number]);
	} while (number--) 
    document.writeln("End of Loop.");
</script>

Run it...   »

Example Result :