Why we use loop in javascript?

loop in JavaScript is used to repeat a block of code multiple times, Instead of writing the same code over and over, a loop allows us to run the same code based on conditions we set, like for a fixed number of times or until something specific happens.

JavaScript Loops Example

For Loop


for (let i = 0; i < 5; i++) {
    console.log(i);
}
// Output: 0, 1, 2, 3, 4

    

While Loop


let i = 0;
while (i < 5) {
    console.log(i);
    i++;
}
// Output: 0, 1, 2, 3, 4

    

Do...While Loop


let i = 0;
do {
    console.log(i);
    i++;
} while (i < 5);
// Output: 0, 1, 2, 3, 4

    
Powered by Blogger.