The do-while Loop
Alright, now let's take a look at the third kind of loop we have in JavaScript: the do-while loop. do-while loops are very similar to while loops, but they're slightly different.
It's easier to show you in code. I'm going to rewrite this logic (displaying odd numbers) using a do-while loop.
-
Just like
whileloops, we have to declare our loop variable externally.let i = 0; -
Note on Scope: Okay, now if we save the changes, we're going to get an error:
Identifier has already been declared.
This is because I have declared
ihere (for thedo-whileloop) and also had it declared for thewhileloop. I cannot redeclare it here. This is different than theithat we have in theforloop. Again, this is all about scoping, and I'm going to talk about that later in the course. -
To make this work, I'm going to temporarily comment out these few lines and rewrite this logic using a
do-whileloop.
-
We add the
dostatement here, then a code block. In this block, we should have our statements. I'm going to borrow them from ourwhileloop (copy and paste them here). -
Finally, at the end of this block, we add the
whilestatement along with our condition (i <= 5), followed by a semicolon.// Assuming 'i' was declared externally, e.g., let i = 0; // (Previous 'while' and 'for' loops are commented out) do { // Statements borrowed from the while loop if (i % 2 !== 0) { console.log(i); } i++; } while (i <= 5);
Key Difference: while vs. do-while
Now, you might be wondering: what is the difference between a while loop and a do-while loop?
Key Takeaway: Execution Order
do-while loops are always executed at least once, even if this condition evaluates to false.
Let me show you what I mean.
Scenario 1: The while loop
-
I'm going to temporarily comment out these few lines and bring back our
whileloop. (Also, we don't need thisforblock for now, so let's comment it out).
-
If you save the changes (with
let i = 0), we get1, 3, 5on the console. -
However, if I change
ito 9:let i = 9; while (i <= 5) { if (i % 2 !== 0) { console.log(i); } i++; } -
We are not going to see anything. Save the changes, and look, there's nothing in the console.

-
Reason: In
whileloops, this condition is evaluated ahead of time—at the beginning of every iteration. The first time we try to execute thiswhileloop, the condition (9 <= 5) evaluates tofalse, so these statements below while loop, are never executed.
Scenario 2: The do-while loop
-
In contrast, in
do-whileloops, this condition is evaluated at the end. And that means these statements are always executed at least once, even if the condition is false. -
Let's try this. I'm going to comment out this
whileloop and changeito 9, just like before.let i = 9; do { if (i % 2 !== 0) { console.log(i); } i++; } while (i <= 5); -
Save the changes.
-
Expected Output:
9 -
Reason: Why? Because in our
do-whileloop here:-
On line 15 (in the video), we check to see if this is an odd number. It is.

-
We display it on the console.
-
Next, we increment
iby 1, soiis 10. -
Then, the condition is evaluated. Of course, it's
false(10 <= 5). So our loop will terminate.
-
Note: Practical Usage
Now, realistically, we're not going to use this do-while a lot in programming. There are situations you may want to use this, but in practical terms, most of the time you will be using a for or while loop. Just be aware of the difference between a while loop and a do-while loop.