for
- loops through a block of code a number of timesfor/in
- loops through the properties of an objectfor/of
- loops through the values of an iterable objectwhile
- loops through a block of code while a specified condition is truedo/while
- also loops through a block of code while a specified condition is truefor (expression 1; expression 2; expression 3) {
// code block to be executed
}
Expression 1 is executed (one time) before the execution of the code block.
Expression 2 defines the condition for executing the code block.
Expression 3 is executed (every time) after the code block has been executed.
for (let i = 0; i < 5; i++) {
console.log(i);
}
for
Loop:for (let i = 0; i < 5; i++) {
console.log(i);
}
while
Loop:let i = 0;
while (i < 5) {
console.log(i);
i++;
}
do-while
Loop:let i = 0;
do {
console.log(i);
i++;
} while (i < 5);
for...in
Loop:let person = {
name: "Vignesh",
age: 19,
job: "Developer"
};
for (let key in person) {
console.log(key, person[key]);
}
for...of
Loop: