- for statement
- do while
- while
- labeled statement
- break statement
- continue statement
- for…in statement
- for…of statement
Loops offer a quick and easy way to do something repeatedly.
for statement
do while
while
labeled statement
A label
provides a statement with an identifier that lets you refer to it elsewhere in your program(引用到别的位置).
The syntax:
label :
statement
For example, you can use a
label
to identify a loop, and then use thebreak
orcontinue
statements to indicate whether a program should interrupt the loop or continue its execution.
markLoop:
while (theMark == true) {
doSomething();
}
The label
markloop
identifies a while loop.
break statement
Use the break
statement to terminate a loop, switch, or in conjunction with (链接到) a labeled statement.
The syntax:
break;
break label;
The first form of the syntax terminates the innermost(最内层) enclosing loop or switch; the second form of the syntax terminates the specified enclosing labeled statement.
continue statement
The continue
statement can be used to restart a while
, do-while
, for
, or label statement
.
The syntax:
continue;
continue label;
Example
checkiandj:
while (i < 4) {
console.log(i);
i += 1;
checkj:
while (j > 4) {
console.log(j);
j -= 1;
if ((j % 2) == 0) {
continue checkj;
}
console.log(j + ' is odd.');
}
console.log('i = ' + i);
console.log('j = ' + j);
}
A statement labeled
checkiandj
contains a statement labeledcheckj
. Ifcontinue
is encountered, the program terminates the current iteration ofcheckj
and begins the next iteration.
If
continue
had a label ofcheckiandj
, the program would continue at the top of thecheckiandj
statement.(如果continue
只有一个label
标记,就会从标记循环语句的上面的代码执行)
for…in statement
The for...in
statement iterates a specified variable over all the enumerable properties(可枚举的属性) of an object.
The syntax:
for (variable in object){
statement;
}
for…of statement
The for...of
statement creates a loop Iterating over iterable objects (including Array
, Map
, Set
, arguments object and so on), invoking a custom iteration hook with statements to be executed for the value of each distinct property (对值的每一个独特的属性调用一个将被执行的自定义的和语句挂钩的迭代).
for (variable of object){
statement;
}
The difference of for … of and for … in
let arr = [3, 5, 7];
arr.foo = 'hello';
for (let i in arr) {
console.log(i); // logs "0", "1", "2", "foo"
}
for (let i of arr) {
console.log(i); // logs 3, 5, 7
}
}
While
for...in
iterates over property names(属性名), `for…of iterates over property values(属性值).