Table of Contents

JavaScript do…while Statement

The JavaScript do…while statement is a loop that executes a block of code at least once before checking a condition. Unlike the while loop, which checks the condition before executing the code, a do...while loop checks the condition after each iteration.

This makes the do...while loop useful when you want a block of code to run at least once, regardless of whether the condition is initially true or false.

JavaScript do…while Syntax

The syntax of the JavaScript do...while loop is:

do {
    // Code to execute
} while (condition);

How it works:

  1. JavaScript executes the code inside the do block.
  2. It checks the specified condition.
  3. If the condition is true, the loop executes again.
  4. If the condition is false, the loop stops.

Notice that the do...while statement ends with a semicolon.

JavaScript do…while Example

Here is a simple example that prints numbers from 1 to 5:

let i = 1;

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

Output:

1
2
3
4
5

In this example, the loop starts with i = 1. It prints the current value and increases it by one. The loop continues while i is less than or equal to 5.

do…while Loop Executes at Least Once

One important feature of the JavaScript do...while statement is that it always executes the code block at least once.

let number = 10;

do {
    console.log("The number is " + number);
} while (number < 5);

Output:

The number is 10

Although the condition number < 5 is false, JavaScript executes the do block once before checking the condition.

JavaScript do…while Loop with Decrement

You can also use a do...while loop to count backward.

let i = 5;

do {
    console.log(i);
    i--;
} while (i >= 1);

Output:

5
4
3
2
1

The loop decreases the value of i after each iteration and stops when the condition becomes false.

JavaScript do…while Loop with an Array

A do...while loop can also be used to access array elements.

const fruits = ["Apple", "Banana", "Mango"];
let i = 0;

do {
    console.log(fruits[i]);
    i++;
} while (i < fruits.length);

Output:

Apple
Banana
Mango

The loop accesses each array element using its index until all elements have been printed.

JavaScript do…while with break Statement

The break statement stops a loop immediately, even if its condition is still true.

let i = 1;

do {
    console.log(i);

    if (i === 3) {
        break;
    }

    i++;
} while (i <= 5);

Output:

1
2
3

When i becomes 3, the break statement terminates the loop.

JavaScript do…while with continue Statement

The continue statement skips the remaining statements in the current iteration and moves to the next condition check.

let i = 0;

do {
    i++;

    if (i === 3) {
        continue;
    }

    console.log(i);
} while (i < 5);

Output:

1
2
4
5

In this example, the number 3 is skipped because the continue statement prevents its console.log() statement from executing.

Difference Between while and do…while Loop

Both while and do...while are JavaScript loop statements, but they differ in when they check their conditions.

Feature while Loop do…while Loop
Condition check Before execution After execution
Minimum execution Zero times One time
Syntax while (condition) do { } while (condition)
Use case Run while a condition is true Run at least once

Example using while:

let a = 10;

while (a < 5) {
    console.log(a);
}

Output: No output, because the condition is false before the first iteration.

Example using do…while:

let b = 10;

do {
    console.log(b);
} while (b < 5);

Output:

10

The do...while loop executes once before checking the condition.

Common Mistakes When Using do…while

Here are some common mistakes to avoid:

1. Forgetting to update the loop variable

let i = 1;

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

This creates an infinite loop because i never changes.

2. Forgetting the semicolon

A do...while statement should end with a semicolon:

let i = 1;

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

3. Using the wrong condition

Always check that the condition correctly represents when the loop should continue. An incorrect condition may cause the loop to stop too early or run indefinitely.

Categorized in:

Javascript, JS Basic,