JavaScript Loops

In any language iterating operation is very important that's why most of the languages support looping structures. Same way JavaScript also supports looping structures to execute same code several times. JavaScript has two options for this "for" statement and "while" statement. We can say "for" loop and "while" loop. When we have an array type and want to perform action on each element then "for" loop or "while" loop can be used. For an example

Example
var loop;
for (loop = 0; loop < 5; loop = loop + 1)
{
    console.log(loop);
}
Above code will print the result as below

0
1
2
3
4

It has the same syntax as C programming language. For statement has 3 parts as below

Initialization: First part is variable initialization, we need to initialize the interator variable loop in this part, in the above example we have initialize it with 0 but we can assign any value. We can declare the variable above "for" statment or inside "for" statment. Condition: Second part is condition, we need to check for the condition, if condition evaluates true then it will execute the code inside for loop else it will complete the loop and execution flow will execute the next statement outside the for loop. Increment/Decrement: Third part is either increment or decrement, everytime it executes the "for" statement it will either increment or decrement the looping variable which will be used to check in the condition next time. We can increment or decrement variable by 1, 2 or any value. We can also shorten the increment part like loop = loop + 1 to loop++.

We will now see how to print all the elements from the array of days using for statement

Example
var dayName = ['Sun', 'Mon', 'Tues', 'Wed', 'Thur', 'Fri', 'Sat'];
var loop;
for (loop = 0; loop < dayName.length; loop++)
{
    console.log(dayName[loop]);
}
Above code will print the result as below

Sun
Mon
Tues
Wed
Thur
Fri
Sat

We have used length property of an array object, length property of array will return the number of elements in the array, In our array there are 7 elements so "for" statement will execute 7 times.

The while statement

The while statement is another way of executing same code multiple times, it's a simple version of the "for" statement. "While" loop contains only 1 part and it's a condition, as long as condition evaluates true then "while" statement will be executed.

Example
var myLoop = 1;
while (myLoop <= 5)
{
    console.log(myLoop);
    myLoop = myLoop + 1;
}
Above code will print the result as below

1
2
3
4
5

Break statements

There are some circumstances when we need to check the condition and decide whether we want to continue iteration or break it, for this we can use break statement. Same way if within loop sometime we want to skip the execution of code block based on some condition then continue statement will be used.

Example
var myLoop = 1;
while (1 == 1)
{
    console.log(myLoop);
	if(myLoop == 5)
	{
		break;
	}
    myLoop = myLoop + 1;
	
}
Above code will print the result as below

1
2
3
4
5

Continue statements

In loop if we want to skip some iteration based on the condition then continue statement will be used, when continue statement gets executed then it will skip the rest of the code in the loop and again start the execution from the begining of the loop. For example

Example
var myloop = 0;
for (var myloop = 0; myloop < 10; myloop++)
{
    if (myloop == 5)
    {
         continue;
    }
    console.log(myloop);
}

Above code will print number upto 9 except number 5

 

← Previous

Next →