Option 1 : The traditional for
-loop
The basics
A traditional for
-loop has three components :
- the initialization : executed before the look block is executed the first time
- the condition : checks a condition every time before the loop block is executed, and quits the loop if false
- the afterthought : performed every time after the loop block is executed
These three components are seperated 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 :
for (;;) {
// Do stuff
}
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);
}
Using a tradtional 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]);
}
Or, if you prefer to loop backwards, you do this :
for (var i = myArray.length - 1; i > -1; i--) {
console.log(myArray[i]);
}
There are, however, many variations possible, like eg. this one :
for (var key = 0, value = myArray[key], var length = myArray.length; key < length; value = myArray[++key]) {
console.log(value);
}
... or this one ...
var i = 0, length = myArray.length;
for (; i < length;) {
console.log(myArray[i]);
i++;
}
... or this one :
var key = 0, value;
for (; value = myArray[key++];){
console.log(value);
}
Whichever works best is largely a matter of both personal taste and the specific use case you're implementing.
Note :Each of these variations is supported by all browsers, including véry old ones!
Option 2 : The 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);
}
Note :Like traditional for
-loops, while
-loops are supported by even the oldest of browsers.
Also, 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);
}
In JavaScript, you can also do this :
for (i in myArray) {
console.log(myArray[i]);
}
This should be used with care, however, as it doesn't behave the same as a traditonal for
-loop in all cases, and there are potential side-effects that need to be considered. See Why is using "for...in" with array iteration a bad idea? for more details.
As an alternative to for...in
, there's now also for for...of
. The following example shows the difference between a for...of
loop and a for...in
loop :
var myArray = [3, 5, 7];
myArray.foo = "hello";
for (var i in myArray) {
console.log(i); // logs 0, 1, 2, "foo"
}
for (var i of myArray) {
console.log(i); // logs 3, 5, 7
}
Note :You also need to consider that no version of Internet Explorer supports for...of
(Edge 12+ does) and that for...in
requires at least IE10.
An alternative to For
-loops is Array.prototype.forEach()
, which uses the following syntax :
myArray.forEach(function(value, key, myArray) {
console.log(value);
});
Note :Array.prototype.forEach()
is supported by all modern browsers, as well as IE9+.
Additionally to the four other options mentioned, jQuery also had its own foreach
variation.
It uses the following syntax :
$.each(myArray, function(key, value) {
console.log(value);
});