Summary of for loop usage in javascript one of the most common ones is for (initial values of loop variables; cyclic conditions; Incremental values) {statement ;}
Example
<Script type = "text/javascript"> for (var x = 0; x <10; x ++) {document. write (x);} script
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
Other usage
(1) omitted expression 1. In this case, the initial value should be assigned to the loop variable before the for statement. Pay attention to the subsequent values.
Example:
<Script type = "text/javascript"> var x = 0; for (; x <10; x ++) {document. write (x) ;}script
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
(2) omitting expression 2, that is, the loop condition loop is not terminated, that is, expression 2 is always true.
Example:
<Script type = "text/javascript"> for (var x = 0; x ++) {document. write (x);} script
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
(3) omit expression 3, but ensure that the loop can end normally.
<Script type = "text/javascript"> for (var x = 0; x <100;) {document. write (x); x ++; // at this time, x ++ is used as a part of the loop body, which is the same as position 3.} script
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
(4) omitted expression 1, 3
<Script type = "text/javascript"> x = 0; for (; x <100;) {document. write (x); x ++;} script
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
This is equivalent to while. You can use for to replace while.
(5) All three expressions are omitted.
<Script type = "text/javascript"> for (;) {document. write ("Js");} script
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
In this way, the loop will not be terminated. Similarly, this small while (true) {} function.
(6) expression 1 can be the initial value of the cyclic variable or other expressions irrelevant to the cyclic variable.
<Script type = "text/javascript"> var x = 0; for (var n = 0; x <100; x ++) {document. write (n); n ++;} script
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
Expressions 1 and 3 can also be comma expressions (including more than one expression, separated by commas );
<Script type = "text/javascript"> for (I = 0, j = 100; I <j; I ++, j --) {r = I + j; document. write (r);} script
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
The value of the comma expression is the rightmost value in the order from left to right.
<Script type = "text/javascript"> for (I = 0; I <100; I ++, I ++) {document. write (I);} script
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
This is
<Script type = "text/javascript"> for (I = 0; I <100; I ++ = 2) {document. write (I);} script
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
(7) The expression can be a logical expression or a character expression. A loop can be executed if it is not false.
Script n = confirm ("FOR") for (; n;) {document. write ("hello");} script
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
From these, we can see that the for and while loops are very powerful.