DEV Community

Atchaya
Atchaya

Posted on

JavaScript While Loop Explained for Beginners

Introduction

When I started learning JavaScript, loops were one of the concepts that confused me.

In this blog, I will explain the while loop in a simple way with examples.

What is a While Loop?

A while loop is used to run a block of code repeatedly as long as a condition is true.

Syntax


javascript
while (condition) {
    // code to execute
}



###Simple Example

Let's print numbers from 1 to 5.

let i = 1;

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


1
2
3
4
5
Enter fullscreen mode Exit fullscreen mode

Top comments (0)