Dot Net For All

while and do..while in JavaScript with Special use case

Hello friends, In this article I will discuss the looping and iteration in JavaScript using do and do.while. I will also discuss a special use case where only these two can be used over other looping mechanisms in JavaScript.

Why do we need looping?

Before understanding while and do.while in JavaScript we should understand why do we need looping any programming language.

Looping helps us to perform a single repetitive or rather iterative work in single block of code. Lets understand it with some examples.

If you want to print numbers from 1 to 10. You can do same as shown in the figure below.

console.log(1);
console.log(2);
console.log(3);
console.log(4);
console.log(5);
console.log(6);
console.log(7);
console.log(8);
console.log(9);
console.log(10);

Above code is doing a repetitive work of writing the number from 1 to 10. This is not how we write programs.

while loop in JavaScript

The same can be done more effectively by using while loop in JavaScript. The syntax of while loop is as shown in the figure below

Initialization
while(comparison){
increment
}

The above syntax can be more clear with with the help of below figure.

while loop in JavaScript

As can be seen in the above figure, I have initialized a variable which can be used to access the elements of the array.

Once the element is accessed I am increasing the value of initialized array. And the loop again compares the value of variable.

Another use case of looping is traversing the elements of array.

Below is an array with some items. If we want to perform any operation on these items one by one. We need to iterate it. In the below code we are only printing the elements of the array.

var array = ["apple", 'banana', 'mango', 'pineapple', 'wateremelon'];
var i = 0;
while(i < array.length){
  console.log(array[i]);
  i++;
}

do in JavaScript

Another way to traverse the collection in JavaScript is to use a do loop. The difference between do and do while is that the do loop will executed at least once.

var array =  ["apple", 'banana', 'mango', 'pineapple', 'watermelon'];
var i = 0;
do{
  console.log(array[i]);
  i++;
}
while(i < array.length)

The problem with the above code is that if the array is empty, we will get an undefined error.

Taking Input from the User in JavaScript

There can be a case where you have to keep getting the input from the user unless some condition is met. In such kind of scenario while loop in JavaScript can he highly useful.

Let see an example of same in the below code snippet.

var array = [];
while(confirm("do you want to add more items?")){
  array.push(prompt("Please enter the item"));
}

for(var item of array)
{
  console.log(item);
}

In the above code I have used the code to add items to the array based on users input. As long as he wants he can keep items to the array.

Conclusion

while and do while are subtle but very important to understand for any beginner. And in this article I have shown their use cases with JavaScript examples.

Top career enhancing courses you can't miss

My Learning Resource

Excel your system design interview