while loop in JavaScript works same like for loop in JavaScript but in a little bit different manner. while loop runs until the condition is true and exits when the condition is false.

Syntax

while (condition)
{
    // statements to be executed here;
}

condition: expression that is to evaluated that decides whether to execute the next iteration of loop. If true, the loop is executed otherwise it terminates the loop.

statements: the code statements that are executed for each condition evaluated to true.

Example

var i=1;    
while(i<=7)
{
    document.write('<font size=\"' + i + '\">Hello World</font><br />');
    i++;
}

Above example shows the use of while loop in JavaScript by executing a single line of code how you can increase the size of font in each loop by incrementing the value of variable i.

No comments yet.

Leave a Comment

All fields are required. Your email address will not be published.

Recent JavaScript Tutorials

JavaScript if-else Ternary Operator

The JavaScript conditional ternary operator is considered as shorthand for if-else statement that is frequently used to return the result in single code statement. It takes three operands to return ...

JavaScript do…while Loop

do...while loop in JavaScript works like while loop in JavaScript the only difference is that do block executes at least once before while loop’s test condition. That’s why do block ...

JavaScript while Loop

while loop in JavaScript works same like for loop in JavaScript but in a little bit different manner. while loop runs until the condition is true and exits when the ...

JavaScript for Loop

for loop in JavaScript is most often used to execute a block of code with different value each time. So, to reduce the code length instead of adding number of ...

JavaScript switch-case Statement

To execute a single block of code satisfying a condition in case statement of JavaScript switch-case is used. break keyword is used in every case statement to exit the switch ...

Recent Comments