A for loop repeats until a specified condition evaluates to false. The JavaScript for loop is similar to the Java and C for loop.
Syntax :-
for (initialization; condition; afterthought)
statement1
example :-
for(let step = 0; step < 5; step++) {
// Runs 5 times, with values of step 0 through 4.
console.log("Walking east one step");
}
The do...while statement repeats until a specified condition evaluates to false. statement is always executed once before the condition is checked.
Syntax :-
do
statement1
while (condition)
example :-
let i = 0;
do{
i += 1;
console.log(i);
} while (i<5)
A while statement executes its statements as long as a specified condition evaluates to true
Syntax :-
while (condition)
statement
Example :-
let n = 0;
let x = 0;
while (n<3){
n++;
x+=n;
}
Use the break statement to terminate a loop or switch
When you use break without a label, it terminates the innermost
enclosing while, do-while, for, or switch immediately and transfers control to the following
statement.
When you use break with a label, it terminates the specified labeled statement.
Syntax :-
break;
break label ;
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 enclosing while, do-while, or for statement and continues execution of
the loop with the next iteration. In contrast to the break statement, continue does not terminate
the execution of the loop entirely. In a while loop, it jumps back to the condition. In a for loop,
it jumps to the increment-expression.
When you use continue with a label, it applies to the looping statement identified with
that label.
Syntax :-
continue;
continue label ;
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.
used in objects .
Syntax :-
for (variable in objects )
statement
Example :-
for (const i in obj) {
result += `${objName}.${i} = ${obj[i]}`;
}
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.
used in arrays .
Syntax :-
for (variable of array )
statement
Example :-
for (const i of array) {
console.log(i);
}