A for
loop repeats until a specified condition evaluates to
false
.
The JavaScript
for
loop is similar to the Java and C
for
loop.
A
for
statement looks as follows:
for ([initialExpression]; [conditionExpression];[incrementExpression])
statement
for
loop executes, the following occurs:
-
The initializing expression
initialExpression
, if any, is executed. This expression usually initializes one or more loop counters, but the syntax allows an expression of any degree of complexity. This expression can also declare variables. -
The
conditionExpression
expression is evaluated. If the value of conditionExpression is true, the loop statements execute. If the value ofcondition
is false, the for loop terminates. (If thecondition
expression is omitted entirely, the condition is assumed to be true.) -
The
statement
executes. To execute multiple statements, use a block statement ({ ... }
) to group those statements. -
If present, the update expression
incrementExpression
is executed. - Control returns to Step 2.
A while
statement executes its statements as long as a specified condition evaluates to
true
.
A while
statement looks as follows:
while (condition)
statement
If the
condition
becomes
false
, statement
within the loop stops executing and control passes to the statement following the loop.
The condition test occurs before
statement in the loop is executed. If the condition returns
true
, statement
is executed and the
condition
is tested again.
If the condition returns
false
,
execution stops, and control is passed to the statement following while
.
The do...while
statement repeats
until a specified condition evaluates to false.
A do...while
statement looks as follows:
do
statement
while (condition);
statement
is always executed once
before the condition is checked. (To execute multiple statements, use a block statement
({ ... }
) to group those statements.)
If condition
is true
,
the statement executes again. At the end of every execution, the condition is checked. When the
condition is false
, execution stops, and control passes to the
statement following do-while
.
The continue
statement can be used to restart a
while
, do-while
,
for
, or label
statement.
-
When you use
continue
without a label, it terminates the current iteration of the innermost enclosingwhile
,do-while
, orfor
statement and continues execution of the loop with the next iteration. In contrast to thebreak
statement,continue
does not terminate the execution of the loop entirely. In awhile
loop, it jumps back to theincrement-expression
. -
When you use
continue
with a label, it applies to the looping statement identified with that label.
Use the break
statement to terminate a loop,
switch
, or in conjunction with a labeled statement.
-
When you use
break
without a label, it terminates the innermost enclosingwhile
,do-while
,for
, orswitch
immediately and transfers control to the following statement. -
When you use
break
with a label, it terminates the specified labeled statement.
The syntax of the break
statement looks like this:
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.
The for...in
statement iterates a specified variable
over all the enumerable properties of an object. For each distinct property,
JavaScript executes the specified statements. A for...in
statement looks as follows:
for (variable in object)
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.
- All the documentation in this page is taken from MDN