While we are discussing loops how can we proceed without exploring the while loop.

The while loop is mostly used when iterations are to be performed an unknown number of times. The for loop can also be used for these purposes however programmers usually prefer the while loop since it gives that conditional feel of looping.

It operates similarly as a for loop does i.e it keeps on going until the specified condition isn't met. So let's jump straight into the syntax.

Syntax of the while loop

while (/*condition*/) {
    // code to be repeated
    // this enclosed block of code is also known as a LOOP BLOCK
  1. condition is the expression to be tested and checked for to execute before executing the loop.
This loop sounds much like english:-
   While the condition is met keep doing this thing!

First while loop

And we never learn without examples!
var num = 3; 
while(num !== 0) {
And let's break it down again:
  1. var num = 3
    is just declaring and initialising a variable.
  2. while(num !== 0)
    starts a while loop definition with the condition inside the parentheses.
    num !== 0
    basically means that the while loop will execute only if this condition is met - var num is not identical to zero.
  3. console.log(num)
    logs the current value of the var num.
  4. num--
    is decrementing the variable num. The expression could have also been --num, num -= 1 or even num = num - 1 but not num - 1 because this doesn't set num to something new. In all loops in programming there has to be some sort of a terminating criteria for the loop.
The output would look like:


So at the start the variable num is 3. It is output, then decremented by 1. num becomes 2. Then the next loop begins and num is again checked if it is equal to zero. Again it is output and decremented and this goes on until it becomes 0 at which point the expression
num !== 0
returns false and hence we come out of the loop.

Now this was some serious explanation for just a simple while loop. Sooner as you move on you will have to tackle complex while loops without such detailed explanation being given.

The do..while loop

The do..while loop isn't anything more than just an extension to the while loop.
do {
    // code to be repeated AT LEAST ONCE
} while(/* condition */)
  1. condition is the expression to be tested and checked for to execute before executing the second iteration.

But why use the do..while loop?

Use this loop when you want to execute the block of code at least once. So even if the condition after the while keyword fails, the block of code is successfully executed once. If it passes then the flow is same as in the while loop.

Consider the following example:
var num = 0; 
do {
} while(num !== 0)
Now this should seemingly log nothing in the console since the specified condition isn't met, but it does. This is due to the do block that executes before any testing.


Things to remember

There are some points to remember when writing while loops.
  1. Unlike a for loop, there are no counter expressions allowed inside the while loop's header. This means that if you want to include a counter in your while loop you have to increment or decrement it inside the loop body.
  2. In a do..while loop the do keyword has to precede the while keyword and be without any parentheses. The parentheses have to go with the while keyword. Failing to do so will result in a syntax error.

In conclusion

Loops are a fundamental part of programming and are used in today's complex computer era to automate a lot of stuff from solving graphs to games. Having a firm understanding on them is not only essential in JavaScript but also in algorithmic computing which is just another amazing journey to explore.