What is loop?
JavaScript loops are control structures that are used for executing a block of code repeatedly as long as the specified condition evaluates true.
Various types of loops
1.for loop
The most common loop, used when the number of iterations is known. It initializes a variable, checks a condition, and updates the variable in a single line.
let star= "";
for (let count = 0; count< 5; count++) {
star = star + "1 "
console.log(star);
}
2. while loop
It executes a block of code as long as a condition is true, checking the condition before each iteration. It is ideal when the number of iterations is unknown.
let count = 0
while (count<5){
console.log(3);
count = count +1;
}
3. do...while loop
It is similar to the while loop, but it guarantees that the code block executes at least once before checking the condition.
do {
console.log(3);
count = count + 1;
} while (6 < 5);
Top comments (0)