Open In App

JavaScript While Loop

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

In JavaScript, a while loop is a control flow statement that repeatedly executes a block of code as long as a specified condition evaluates to true.

While Loop Syntax:

while (condition) {
Code block to be executed
}

While Loop Example:

Here’s an example of a while loop that counts from 1 to 5.

JavaScript




let count = 1;
while (count <= 5) {
  console.log(count);
  count++;
}


Output

1
2
3
4
5

Explanation:

Here,

  • Count is initialized to 1.
  • The loop runs as long as count is less than or equal to 5.
  • Inside the loop, console.log(count) outputs the current value of count.
  • After each iteration, count is incremented by 1 (count++).

Do-While loop

A Do-While loop is another type of loop in JavaScript that is similar to the while loop, but with one key difference: the do-while loop guarantees that the block of code inside the loop will be executed at least once, regardless of whether the condition is initially true or false.

Do-While loop Syntax:

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

Do-While loop Example:

Here’s an example of a do-while loop that counts from 1 to 5.

JavaScript




let count = 1;
do {
  console.log(count);
  count++;
} while (count <= 5);


Output

1
2
3
4
5

Explanation:

Here,

  • count is initialized to 1.
  • The block of code inside the loop is executed first.
  • Then, the condition count <= 5 is checked. If the condition is true, the loop will continue to execute. If it’s false, the loop will terminate.
  • The loop will output the numbers 1 through 5, just like the while loop.

Comparison between the while and do-while loop:

The do-while loop executes the content of the loop once before checking the condition of the while loop. While the while loop will check the condition first before executing the content.

While Loop

Do-While Loop

It is an entry condition looping structure.

It is an exit condition looping structure.

The number of iterations depends on the condition mentioned in the while block.

Irrespective of the condition mentioned in the do-while block, there will a minimum of 1 iteration.

The block control condition is available at the starting point of the loop.

The block control condition is available at the endpoint of the loop.

Comparison between the while and for loop:

Both while and for loops are used for repetitive tasks in JavaScript, but they have different syntax and are typically used in different scenarios.

  1. Initialization:
    • While Loop: The initialization of variables happens before the loop.
    • For Loop: The initialization of variables is done within the loop syntax.
  2. Condition:
    • Both loops require a condition that determines whether the loop should continue or terminate.
  3. Increment/Decrement:
    • While Loop: The increment/decrement of loop control variable(s) must be done manually within the loop block.
    • For Loop: The increment/decrement of loop control variable(s) is part of the loop syntax.
  4. Use Cases:
    • While Loop: Typically used when you don’t know the number of iterations in advance or when you want to create an infinite loop.
    • For Loop: Generally used when you know the number of iterations in advance or when iterating over arrays or other collections.

For loop Example:

Here, the for loop is more concise and keeps the initialization, condition, and increment/decrement within a single line,

Javascript




for (let count = 0; count < 5; count++) {
  console.log(count);
}


Output

0
1
2
3
4



Last Updated : 04 Mar, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads