Standard for loops

suggest change

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

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents