CKTIS / JavaScript
Loops and iteration
Loops repeat statements according to a condition or successive elements. Learn how for, while, do...while, for...of, and for...in differ, and how break and continue control execution.
Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support
Syntax
for (let i = 0; i < limit; i++) { /* body */ }
while (condition) { /* body */ }
do { /* body */ } while (condition);
for (const value of iterable) { /* body */ }
for (const key in object) { /* body */ }A loop repeats a statement body. while tests its condition before each execution, so its body may never run. do...while tests afterward, so its body runs at least once. Conditions are converted to Boolean values.
A classic for initializes once, then tests its condition, executes its body, and performs its update. An omitted condition means true. Ensure termination is reachable; an incorrect update can cause an infinite loop. For array indices, normally use i < values.length, not i <= values.length.
for...of retrieves values from an iterable, such as an array. for...in enumerates enumerable string-keyed property names, including inherited ones; it is not for retrieving array values. const in a for...of header creates a new binding each iteration but does not freeze an object.
Without a label, break exits the nearest loop or switch, while continue skips the rest of the current iteration of the nearest loop. In a classic for, the update still runs after continue. The example sums 2 and 8, skips 5, and exits at 11.
Examples
const values = [2, 5, 8, 11];
let total = 0;
for (const value of values) {
if (value > 8) break;
if (value % 2 !== 0) continue;
total += value;
}
if (total !== 10) throw new Error("Unexpected total");
console.log(total);Result
10
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.