Three ways of writing a loop:
<!doctype html>
<title>js cycle by cloud-dwelling community </title>
<meta charset= "Utf-8"/> <meta
Name= "keywords" content= "JS cycle by cloud-dwelling community"/> <meta name= "description"
content= "JS cycle by cloud-dwelling community"/>
Different types of loops
JavaScript supports different types of loops:
for-loop code block a certain number of times
for/in-Looping through the properties of an object
while-loops the specified code block when the specified condition is true
Do/while-also loops the specified code block when the specified condition is true
For loop
A For loop is a tool that you will often use when you want to create a loop.
The following is the syntax for the FOR loop:
for (statement 1; Statement 2; Statement 3)
{
The code block that was executed
}
Statement 1 executes before the Loop (code block) starts
Statement 2 defines conditions for running loops (code blocks)
Statement 3 executes after the Loop (code block) has been executed
Instance
Copy Code code as follows:
for (var i=0; i<5; i++)
{
X=x + "The number is" + i + "<br>";
}
Give it a shot yourself.
From the example above, you can see:
Statement 1 Sets the variable (var i=0) before the loop begins.
Statement 2 defines the conditions for a circular operation (I must be less than 5).
Statement 3 Adds a value (i++) after each block of code has been executed.
Statement 1
In general, we use statement 1 to initialize the variable (var i=0) used in the loop.
Statement 1 is optional, that is to say, no statement 1 is used.
You can initialize any (or more) of the values in statement 1:
Instance:
Copy Code code as follows:
for (var i=0,len=cars.length; i<len; i++)
{
document.write (Cars[i] + "<br>");
}
You can also omit statement 1 (for example, when a value has been set before the loop starts):
Instance:
Copy Code code as follows:
var i=2,len=cars.length;
for (; i<len; i++)
{
document.write (Cars[i] + "<br>");
}
Statement 2
Usually statement 2 is used to evaluate the condition of the initial variable.
Statement 2 is also optional.
If statement 2 returns True, the loop starts again, and if False is returned, the loop ends.
Tip: If you omit statement 2, you must provide a break within the loop. Otherwise the loop will not stop. This can cause the browser to crash. Read the contents of the break in later chapters of this tutorial.
Statement 3
Typically, statement 3 increases the value of the initial variable.
Statement 3 is also optional.
Statement 3 has several uses. The increment can be negative (i--) or larger (i=i+15).
Statement 3 can also be omitted (for example, when there is a corresponding code inside the loop):
Instance:
Copy Code code as follows:
var i=0,len=cars.length;
for (; i<len;)
{
document.write (Cars[i] + "<br>");
i++;
}
For/in Cycle
The JavaScript for/in statement loops through the properties of the object:
Instance
Copy Code code as follows:
var person={fname: "John", lname: "Doe", age:25};
for (x in person)
{
Txt=txt + person[x];
}
You'll learn more about the For/in cycle in the chapters on JavaScript objects.
can refer to this article concretely: http://www.jb51.net/w3school/js/js_loop_for.htm