09. BASICS | Loops in JavaScript
This article will talk about looping techniques in javascript.
for loop
Loops a block of code number of times.
for (statement1; statement2; statement3) {
// code to be executed
}
statement1is executed for one time only.statement2is the condition based on which the loop runs (loop body is executed).statement3is executed every time the loop body is executed.
while loop
Loops a block of code based on a specific condition.
while (condition) {
// code to be executed
}
condition never becomes false, the loop will never end and this might crash the runtime.do - while loop
whileloop variant which runs atleast once.
do {
// code to be executed ==> executed atleast once
} while (condition)
break and continue
The
breakstatement immediately terminates the current loop orswitchstatement and transfers control to the statement following the terminated one. It is commonly used when a specific condition is met and there is no need to continue with the remaining iterations or cases.
The
continuestatement skips the current iteration of a loop and moves directly to the next one, without terminating the loop entirely.
Happy reading!

