Loops in javaScript - for, while, do while, for-in


Loops in javaScript are mostly similar to the other programming languages but here we are not going for other programming languages but for Javascript, so in this article we describe how you can work loops in javascript. Commonly known loops are - for, while, do while, for-in

Loop or Itration Concept in JavaScript is very much similar to C, C++, PHP or Java programming language or may be more. Here we are going to discuss the Loop types in JavaScript referene only. The commonly known loop types are For, while and do-while, so our focus will be on these three, beside them JavaScript provide another type of loop called for-in loop, this is usually applied on the array or objects, we will also display in the end of this article how to use this type of loop. So let's get started now.

For Loop

In for loop we pass three elements these are initialization, condition expression, increment/decrement of loop counter. the form is simple, and the flow is like, first of all the loop is initialize and in next step the condition is checked, if the condition expression results in true the control enters the loop body and executes the code, finally moving towards the end the counter increment or decrement is performed.

After doing the whole process once, in next step the initialization is skipped, so it is one time process, in the following program we are creating a simple loop which is going to print numbers from 1 to 10;


for(i=1;i<=10;i++){
  console.log(i);
}

Output:
1
2
3
4
5
6
7
8
9
10

Well, this could be the simplest example of for loop, in this the value of counter is first initialized from i=1, and then condition is checked, i.e. i < 10, so the loop will remain active unless the value of i is more than 10, so this condition makes the last step important. Unless we want the loop to remain active forever, we will have to do increment, so that at one level the counter crosses the 10 and value of condition expression is false, which will break the loop.

While Loop

The while loop is not too much different from the for loop but in this case the operation is kind of different, however the operation and process remains the same. Initialization, condition and counter altration. following is the same example as above just perfromed by the while loop instead for.


i = 1;
while(i<=10){
	console.log(i);
	i++;
}

Output:
1
2
3
4
5
6
7
8
9
10

Note: If you are running the code on the firebug console, as we do while testing the code, in the result output above you may see another 10 printed on the console, that is not because of loop but the value of i after finishing up the loop, that is a console related issue not program related.

So, you can see we have only changed the position or say placement of the elements and a while has replaced the for, so this is all you need to know about while loop, it is simple as for loop.

Do-While

There may arise some situation where you want your loop to run once, no matter what, so to fulfill such kind of condition we are provided with the do-while loop. In do while we place the condition evaluation after the loop body, so we have a gurantee that the loop will execute at least once. The structure of the do while is as follows:


do{
	console.log("something");
}
while(false);

Output:
something

the expression or say value in the while is false yet the loop has run once, this is what the do-while is for, it will always run once, no matter what is the expression in while statement. we you want a more generalize example, now we convert our original number printing program into do-while loop.


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

output:
1
2
3
4
5
6
7
8
9
10

So again we have the same result. at last as promised we are going to demonstrate another kind of handy looping concept of javascript, which can be really handy when working with the objects or arrays. this construct is called For-In loop.

For-In Loop

In for in loop we pass either an array or an object as parameter and we pass another parameter which acts as the pointer to the current index in the array or object. What we mean here is, we pass an array to the for-in and it repeats itself over the elements of that array or object one by one, thus reduce the effort to write extra code.


i = [1,2,3,4,5];

for(x in i){
  console.log(i[x]);
}

Output:
1
2
3
4
5

In this program we see that there is an array and when we write, x in i that means we are getting current-index of array i in x, thus we can use this information for the further processing. This is all we have to say on the looping concept of the JavaScript, in case you have any query please feel free to give comments below.