The js for loop can execute a code block as specified number of times.
JavaScript Loop
We can output the values of the array like this: if you want to run the same code over and over, and the values are different each time, then using loops is convenient.
document.write(cars[0] + "<br>");document.write(cars[1] + "<br>");document.write(cars[2] + "<br>");document.write(cars[3] + "<br>");document.write(cars[4] + "<br>");document.write(cars[5] + "<br>");document.write(cars[5] + "<br>");
But we write this
for (var i=0; i<cars.length;i++){document.write(cars[i]+"<br>");}Example: Output 1-100 numbers
for(var i=0;i <=100;i++){document.write(i+"<br>")}For is a pre-test loop, and variables can be initialized before the loop, and the code to be executed after the loop is defined. The syntax is as follows
for(inintialization;expression;psot=loop-expression) statement
The execution process is as follows:
1. Execute initialization statement
2. Determine whether the expression is true, if so, continue, otherwise terminate the entire loop body.
3. Execute the loop body statement code
4. Execute the post-loop-expression code
5. Return to step 2
The most commonly used form of for loop is for(var i=0; i<n;i++){statement}
It means that a loop is executed in total n times, which is very suitable for known loop count operations.
The code copy is as follows:
var aNumbers = new Array();
var sMessage = "You entered:/n";
var iTotal = 0;
var vUserInput;
var iArrayIndex = 0;
do{
vUserInput = prompt("Enter a number, or '0' exit","0");
aNumbers[iArrayIndex] = vUserInput;
iArrayIndex++;
iTotal += Number(vUserInput);
sMessage += vUserInput + "/n";
}while(vUserInput != 0) // Exit the loop body when the input is 0 (default value).
sMessage += "Total:" + iTotal;
document.getElementById("xxx").innerHTML=sMessage;
Different types of loops
JavaScript supports different types of loops:
for - Loop code blocks for a certain number of times
for/in - Loop through the properties of an object
while - Loop the specified code block when the specified condition is true
do/while - Similarly, loops the specified code block when the specified condition is true
For loop
for loops are tools you often use when you want to create loops.
Here is the syntax of the for loop:
for (Sentence 1; Statement 2; Statement 3)
{
Executioned code block
}
Statement 1 is executed before the loop (code block) begins
Statement 2 Defines the conditions for running a loop (code block)
Statement 3 Executes after the loop (code block) has been executed
Example
for (var i=0; i<5; i++) { x=x + "The number is " + i + "<br>"; }From the example above, you can see:
Statement 1 Set the variable (var i=0) before the loop starts.
Statement 2 defines the conditions for loop running (i must be less than 5).
Statement 3 Adds a value (i++) after each code block has been executed.
Statement 1
Usually we use statement 1 to initialize the variable used in the loop (var i=0).
Statement 1 is optional, which means that statement 1 is not used.
You can initialize any (or multiple) values in statement 1:
Example:
for (var i=0,len=cars.length; i<len; i++){document.write(cars[i] + "<br>");}You can also omit statement 1 (for example, when the value has been set before the loop starts):
Example:
var i=2,len=cars.length;for (; i<len; i++){document.write(cars[i] + "<br>");}Statement 2
Typically Statement 2 is used to evaluate the conditions for 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 break within the loop. Otherwise the cycle will not be stopped. This may crash the browser. Please read about break in the later chapters of this tutorial.
Statement 3
Usually statement 3 will increase the value of the initial variable.
Statement 3 is also optional.
Statement 3 has many uses. The increment can be a negative number (i--), or larger (i=i+15).
Statement 3 can also be omitted (for example, when there is corresponding code inside the loop):
Example:
var i=0,len=cars.length;for (; i<len; ){document.write(cars[i] + "<br>");i++;}For/In loop
JavaScript for/in statement loops through the properties of an object:
Example
var person={fname:"John",lname:"Doe",age:25};for (x in person) { txt=txt + person[x]; }You will learn more about for/in loops in the chapter on JavaScript Objects.
While loop
Click on the while loop and do/while loop to view this article.
The above is all about the for loop in JavaScript. I hope you like it.