JavaScript for Beginners
About Lesson

While Loop is a control flow statement that allows a part of the code to be executed repeatedly based on a given Boolean condition. The While Loop can be thought of as a repeating if statement.


Syntax of a While Loop

The syntax of a While Loop in JavaScript is as follows:

while (condition) {
   // code to be executed


  • Condition: This is a Boolean expression that is checked before each iteration of the loop. If it evaluates to true, the loop continues; if it evaluates to false, the loop ends.


Example of a While Loop

Here’s a simple example of a While Loop that prints the numbers from 1 to 5:

let i = 1;
while (i <= 5) {


In this example, i is initialized to 1, the condition checks if i is less than or equal to 5, and i is incremented by 1 at the end of each loop iteration. The console.log(i) inside the loop will print the numbers 1 through 5.


The While Loop is another powerful tool in JavaScript, allowing you to write more efficient and cleaner code. It’s a fundamental concept that’s widely used in programming, so understanding how it works will greatly enhance your JavaScript coding skills.