javascript

While loop in Javascript with examples

In JavaScript, a while loop is a control flow statement that allows you to execute a block of code repeatedly as long as a certain condition is true. In this post we will explain the while and do-while loop of Javascript with examples.

let counter = 1;

while (counter <= 5) {
    console.log("counter is: " + counter);
    counter++;
}

Below is the syntax of the while loop in Javascript.

while (condition) {
   // code block
}

The code block will be executed in each iteration of the while loop until the given condition is true.

Example

let counter = 1;

while (counter <= 5) {
    console.log("counter is: " + counter);
    counter++;
}

Output

counter is: 1
counter is: 2
counter is: 3
counter is: 4
counter is: 5

Try it yourself

In the above code example, the while loop will continue to run as long as the value of the counter is less than or equal to 5.
Each time the loop runs, the value of the counter variable is increased by 1. So the first time the loop runs, the counter is 1, the second time it is 2, and so on.

Remember that if you will now increase the value of the counter, the loop will try to execute the code block infinite times as the condition will always be true. This may crash your web browser or make the web page unresponsive.

When the counter reaches 11, the loop will stop running because the condition counter <= 5 will no longer be true.


Do-while loop in Javascript

In JavaScript, a do-while loop is similar to a while loop, except that the loop will always run at least once, even if the condition is initially false.

Syntax

do {
    // code block
} while (condition);

Here is an example of how you might use a do-while loop:

let counter = 1;

do {
    console.log("Counter is: " + counter);
    counter++;
} while (counter <= 5);

Output

Counter is: 1
Counter is: 2
Counter is: 3
Counter is: 4
Counter is: 5

In the above example, the code inside the loop will run once before the condition is checked. If the condition (count <= 5) is true, the loop will continue to run. Otherwise, the loop will stop.

One important thing to note about do-while loops is that the condition is checked at the bottom of the loop after the code inside the loop has run. This means that the code inside the loop will always run at least once, even if the condition is initially false.

In contrast, with a while loop, the condition is checked at the top of the loop, before the code inside the loop runs. So if the condition is initially false, the code inside the while loop will never run.


Example - Calculate the sum of numbers from 0 to 10 (while loop)

Here, we'll demonstrate how to use a while loop to iterate through the range of numbers from 0 to 10 and calculate the sum of those numbers.
We'll also demonstrate how to use the loop to calculate the sum of any range of numbers. So let's get started!

let sum = 0;
let i = 0;

while (i <= 10) {
    sum += i;
    i++;
}

console.log("Sum of numbers from 0 to 10: " + sum);

Output

Sum of numbers from 0 to 10: 55

Try it yourself

This code example uses a while loop to calculate the sum of numbers from 0 to 10. The variable sum is initialized to 0 and the variable i is initialized to 0.
The while loop will run as long as i is less than or equal to 10, incrementing the value of i by 1 each time.
On each iteration of the loop, the current value of i is added to the sum variable. Once the loop is finished, the variable sum will hold the total value of all the numbers from 0 to 10, which is then logged to the console.


Print all even numbers from 0 to 10 using a while loop

In this example, we will use a while loop to iterate through a loop and print out all of the even numbers between 0 and 10.

let i = 0;

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

Output

0
2
4
6
8
10

This code example will log every number from 0 to 10, increasing in increments of 2. The variable "i" is set to 0 and will increment by 2 each time the loop runs until it reaches 10. The loop will log the value of "i" to the console each time it runs.


Was this helpful?