JavaScript while loop

Loops are used to execute code multiple times until the specified condition evaluates to true.

While loop

The while loop creates a code block of statements which is executes until the condition evaluates to true. It stops executing loop once evaluates to false. The condition is checked before executing the loop code block. If condition is evaluates to false, the loop code block will never executes.

Syntax

while(condition) {
    // loop code block of statements
}

condition A valid JavaScript expression or a Boolean value. It the condition is evaluates to true, the loop code block will execute. If the condition evaluates to false the code after the while loop will continue executing

statement The series of statements to execute if the condition evaluates to true. If the condition is evaluates to false, this statements are ignored.

Example

<!DOCTYPE html>
<html>
<body>

Odd numbers upto 50:
<p id="text"></p>

<script>

var series = '', i = 0;
while(i < 50) {
    if(i % 2 > 0) {
        series += i+'<br >';
    }

    i++;
}

document.getElementById('text').innerHTML = series;
</script>

</body>
</html>

Download the code Run the code

Above while loop prints odd numbers up to 50.

When the while loop encounters in the code, the value of variable i is checked if its < 50 or not and increment value of i on each iteration

Note
Please take care of variables used inside condition for while loop, if the condition never evaluates to true than the loop will continue infinite number of times, causes browser to slow down due to memory usage.

The do while loop is another version on a while loop

The do/while loop

The do while loop statement create a loop that is execute at-least once and than continue until a condition evaluates to false. Unlike while loop where condition is evaluates in beginning, for do/while loop the condition is evaluates at the end i.e. after the loop code block executes.

Syntax

do {
    // loop code block of statements
} while (condition);

statement The series of statements to execute if the condition evaluates to true. If the condition is evaluates to false, this statements are ignored.

condition A valid JavaScript expression or a Boolean value. It the condition is evaluates to true, the loop code block will execute. If the condition evaluates to false the code after the while loop will continue executing

Example

<!DOCTYPE html>
<html>
<body>

Odd numbers upto 50:
<p id="text"></p>

<script>

var series = '', i = 0;
do {
    if(i % 2 > 0) {
        series += i+'<br >';
    }

    i++;
} while(i < 50) 

document.getElementById('text').innerHTML = series;
</script>

</body>
</html>

Download the code Run the code

Above loop statements executes at-least once irrespective of value of condition variable. i.e even condition is false the code executes one time.

Like while loop, do/while loop can be infinite if the condition never evaluates to false

You can use any loop statements based on your requirement. Most often either while loop or for loop can be used as if initialization and expression part are removed from for loop than it is similar to while loop with condition and increment within loop code statements.

<<< JavaScript For loopJavaScript Break and Continue >>>
Copyright 2005-2016 KnowledgeHills. Privacy Policy. Contact .