MOCKSTACKS
EN
Questions And Answers

More Tutorials









Javascript Iteration

A traditional for-loop
A traditional for loop has three components:
1. The initialization: executed before the look block is executed the first time
2. The condition: checks a condition every time before the loop block is executed, and quits the loop if false
3. The afterthought: performed every time after the loop block is executed.

These three components are separated from each other by a ; symbol. Content for each of these three components is optional, which means that the following is the most minimal for loop possible:
Of course, you will need to include an if(condition === true) { break; } or an if(condition === true) { return; } somewhere inside that for-loop to get it to stop running.
Usually, though, the initialization is used to declare an index, the condition is used to compare that index with a minimum or maximum value, and the afterthought is used to increment the index:


for (var i = 0, length = 10; i < length; i++) {
 console.log(i);
}

Output

0
1
2
3
4
5
6
7
8
9

Using a traditional for loop to loop through an array

The traditional way to loop through an array, is this:


for (var i = 0, length = myArray.length; i < length; i++) {
console.log(myArray[i]);
}

Output

0
.
.
.
.
myArray.lenght

Or, if you prefer to loop backwards, you do this:


for (var i = myArray.length - 1; i > -1; i--) {
 console.log(myArray[i]);
}

Output

5
4
3
2
1

There are, however, many variations possible, like for example this one:


var myArray = [1,2,3,4,5];
for (var key = 0, value = myArray[key], length = myArray.length; key < length; value =
myArray[++key]) {
 console.log(value);
}

Output

1
2
3
4
5

A while loop

One alternative to a for loop is a while loop. To loop through an array, you could do this:


var key = 0;
while(value = myArray[key++]){
 console.log(value);
}

Output

1
2
3
4
5

Like traditional for loops, while loops are supported by even the oldest of browsers.
Also, note that every while loop can be rewritten as a for loop. For example, the while loop hereabove behaves the exact same way as this for-loop:


for(var key = 0; value = myArray[key++];){
 console.log(value);
}

Output

1
2
3
4
5


Conclusion

In this page (written and validated by ) you learned about Javascript Iteration . What's Next? If you are interested in completing Javascript tutorial, your next topic will be learning about: Javascript Destructuring an array.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.