The for-loop:

Many loops have the form:

initilize some index variable(s)
while (test expression) {
  statements
  increment index variable(s)
}

so a specific type of loop was created to handle this form specifically, it is the "for" loop:

for(init expr; test expr; inc expr) statement

All three expressions are optional and may be omitted, however the ;'s must remain present. i.e.:

for(;;) statement; // Will loop forever.

init expr is only performed once. You may declare variables using var inside this expression.

test expr is performed at the beginning of each loop, performing the statement only if this is true. If omitted it is always true.

inc expr is performed after the statement (or compound statement) at the end of each loop. This is always executed even if a continue is raised.

Examples:

// print 0 to 9: for(var i=0; i < 10; i++) { console.log(i); }

// print 10 down to 0: for(i=10; i >= 0; i--) console.log(i);

/** * print from 'start' to 'end', skipping 'skip' steps after each output. */ for(i=start; i < end; i+=skip) console.log(i);

/** * Prints n as a binary string. */ function binary(n) { var b = []; for (var i = 0; n > 0; i++) { b[i] = n % 2; n = Math.floor(n / 2); } return b.reverse().join(""); }

// Given an array a: var a = [1, 5, 20, -6, 35, -100, 50, 40, 16]; // Find the largest element of a: var max = a[0]; for(var i=1; i < a.length; i++) { if (max < a[i]) max = a[i]; } console.log("The largest element in a is: " + max);

// Find the second largest element: var max = Math.max(a[0], a[1]); var second = Math.min(a[0], a[1]); for(var i = 2; i < a.length; i++) { if (max < a[i]) { second = max; max = a[i]; } if (second < a[i] && a[i] < max) second = a[i]; } console.log("The second largest element of a is: " + second);

// Prints 3, 4, 5 and 6 only: for(i=0; i < 10; i++) { if (i < 3) continue; if (i > 6) break; console.log(i); }

// Count the number of times the character 's' is found in str: var str = "some string"; var ss = 0; for(var i=0; i < str.length; i++) { if (str[i] == 's') ss++; } console.log("# of S's = " + ss);

/** * Returns the sub-string starting at offset and up to length bytes (or the * end of the string if not given. */ function substr(str, offset, length) { // If length is "undefined" it wasn't passed to the function so we give it // a default value: if (typeof length == "undefined") length = str.length; // If offset is not within the string, return an empty string: if (offset < 0 || str.length <= offset) return ""; // Loop copies characters from str starting at offset var s = ""; for( ;str[offset] !== undefined && length > 0; offset++, length--) { s += str[offset]; } return s; }

/** * Reverse the string s: */ function reverse(s) { var r = ""; for(var i=s.length-1; i >= 0; i--) { r += s[i]; } return r; }

/** * Returns the index of "needle" in "haystack" or -1 if not found. */ function locate(haystack, needle) { var i = 0; for(; i < haystack.length; i++) { if (haystack[i] === needle) return i; } return -1; }