Affiliate link

Node.js Controls - Loops

In Node.js, control loops are used to repeatedly execute a block of code until a certain condition is met. Control loops are essential for creating dynamic and responsive applications that can react to changing data or user input.

Types of control loops that you can use in Node.js

For Loops

for (initialization; condition; increment/decrement)

for (initialization; condition; increment/decrement) {
    // code to be executed
}

for (let i = 1; i <= 5; i++) {
    console.log(i);
}

Nested For Loops

for (let i = 1; i <= 10; i++) {
    for (let j = 1; j <= 10; j++) {
        console.log(i + " * " + j + " = " + i * j);
    }
}

While Loops

while (condition) {
    // code to be executed
}

let i = 1;

while (i <= 5) {
    console.log(i);
    i++;
}

Do-While Loops

do {
    // code to be executed
} while (condition);

let i = 1;

do {
    console.log(i);
    i++;
} while (i <= 5);
Affiliate link
Affiliate link
Advertisment
Fiverr
Ad - Managed WordPress Hosting by SiteGround - Powerful, yet simple to use. Amazing speed, tools and support. Click for more.