Latest web development tutorials
×

JavaScript Tutorial

JavaScript Tutorial JavaScript Introduction JavaScript usage JavaScript Output JavaScript grammar JavaScript Statement JavaScript Notes JavaScript variable JavaScript type of data JavaScript Object JavaScript function JavaScript Scope JavaScript event JavaScript The string JavaScript Operator JavaScript Comparison JavaScript If...Else JavaScript switch JavaScript for JavaScript while JavaScript Break & Continue JavaScript typeof JavaScript Type conversion JavaScript Regular Expressions JavaScript error JavaScript debugging JavaScript Variable promotion JavaScript Strict mode JavaScript Use errors JavaScript Form validation JavaScript Keep the keyword JavaScript JSON JavaScript void JavaScript Code specification

JS function

JavaScript Function definition JavaScript Function parameter JavaScript Function call JavaScript Closure

JS HTML DOM

DOM Introduction DOM HTML DOM CSS DOM event DOM EventListener DOM element

JS Advanced Tutorial

JavaScript Object JavaScript Number JavaScript String JavaScript Date JavaScript Array JavaScript Boolean JavaScript Math JavaScript RegExp Object

JS Browser BOM

JavaScript Window JavaScript Window Screen JavaScript Window Location JavaScript Window History JavaScript Navigator JavaScript Pop-ups JavaScript Timing events JavaScript Cookies

JS Library

JavaScript Library JavaScript test jQuery JavaScript test Prototype

JS Examples

JavaScript Examples JavaScript Object instance JavaScript Browser object instance JavaScript HTML DOM Examples JavaScript to sum up

JS Reference Manual

JavaScript Object HTML DOM Object

JavaScript while loop

As long as a specified condition is true, the loop can always execute the code block.


while loop

while true loop will loop code block is executed at the specified conditions.

grammar

while ( 条件 )
{
需要执行的代码
}

Examples

In this case the loop will continue to run as long as the variable i is less than 5:

Examples

while (i<5)
{
x=x + "The number is " + i + "<br>";
i++;
}

try it"

lamp If you forget to add conditions to the value of the variables used in the cycle never ends. This can cause the browser to crash.


do / while loop

do / while loop is a variant of the while loop. The cycle will check whether the conditions are true before executing a block of code, and if the condition is true, it will repeat the cycle.

grammar

do
{
需要执行的代码
}
while ( 条件 );

Examples

The following example uses the do / while loop. The loop will execute at least once, even if the condition is false it will be executed once, because the code block will be executed before the condition is tested:

Examples

do
{
x=x + "The number is " + i + "<br>";
i++;
}
while (i<5);

try it"

Do not forget to increase the value of the variable conditions used, otherwise the cycle will never end!


Compare for and while

If you've read the previous chapter for details about the loop, you will find that while loop like the for loop.

In this example the recycling loop for cars to display all values in the array:

Examples

cars=["BMW","Volvo","Saab","Ford"];
var i=0;
for (;cars[i];)
{
document.write(cars[i] + "<br>");
i++;
}

try it"

In this example of recycling while loop to display all the values in the array of cars:

Examples

cars = [ "BMW", "Volvo", "Saab", "Ford"];
var i = 0;
while (cars [i])
{
document.write (cars [i] + "<br>");
i ++;
}

try it"