Skip to main content

Command Palette

Search for a command to run...

09. BASICS | Loops in JavaScript

This article will talk about looping techniques in javascript.

Updated
2 min readView as Markdown
S
Full-Stack Developer with 4 years' experience, specializing in backend development. Skilled in JavaScript, React, Python, Databases, and AWS. Known for building scalable web apps, leading teams, and maintaining strong client communication. Upskilling in Generative AI.

for loop

Loops a block of code number of times.

for (statement1; statement2; statement3) {
    // code to be executed
}
  • statement1 is executed for one time only.

  • statement2 is the condition based on which the loop runs (loop body is executed).

  • statement3 is 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
}
💡
If the condition never becomes false, the loop will never end and this might crash the runtime.

do - while loop

while loop variant which runs atleast once.

do {
    // code to be executed   ==> executed atleast once
} while (condition)

break and continue

The break statement immediately terminates the current loop or switch statement 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 continue statement skips the current iteration of a loop and moves directly to the next one, without terminating the loop entirely.


Happy reading!