JavaScript Standard "for" loops


Example

Standard usage

for (var i = 0; i < 100; i++) {
    console.log(i);
}

Expected output:

0
1
...
99

Multiple declarations

Commonly used to cache the length of an array.

var array = ['a', 'b', 'c'];
for (var i = 0; i < array.length; i++) {
    console.log(array[i]);
}

Expected output:

'a'
'b'
'c'

Changing the increment

for (var i = 0; i < 100; i += 2 /* Can also be: i = i + 2 */) {
    console.log(i);
}

Expected output:

0
2
4
...
98

Decremented loop

for (var i = 100; i >=0; i--) {
    console.log(i);
}

Expected output:

100
99
98
...
0