Programming Search⌘K

Featureintro

Loops and iteration

Also called JavaScript loops, iteration statements, for loop, while loop, for...of, for...in, looping over an array.

JavaScript repeats code with for, while, do...while, for...in and for...of, plus break, continue and labels. for...of walks the values of an iterable, and for...in walks property names.

Language
JavaScript

Synopsis

const fruits = ["apple", "pear", "plum"];

for (let i = 0; i < fruits.length; i++) console.log(i, fruits[i]);
for (const fruit of fruits) console.log(fruit);
for (const key in { a: 1, b: 2 }) console.log(key);

let n = 0;
while (n < 2) n++;
do n += 10; while (n < 5);
console.log(n);
Output
0 apple
1 pear
2 plum
apple
pear
plum
a
b
12
Statement Repeats Use it for
for (init; cond; next) while cond is true a counter or an index
while (cond) while cond is true, tested first an unknown number of passes
do ... while (cond) at least once, tested after a body that must run once
for (x of iterable) for each value of an iterable arrays, strings, maps, sets
for (k in object) for each enumerable string key the property names of a plain object
break, continue, label: stop the loop, skip a pass, name a loop early exit, nested loops

Semantics

r1. "Loops offer a quick and easy way to do something repeatedly."1 MDN lists the statements: "for statement do...while statement while statement labeled statement break statement continue statement for...in statement for...of statement".1 In the specification, an IterationStatement is one of DoWhileStatement, WhileStatement, ForStatement and ForInOfStatement.2

r2. "A for loop repeats until a specified condition evaluates to false."1 If the condition is left out, "the condition is assumed to be true."1 A while loop tests first: "The condition test occurs before statement in the loop is executed."1 In a do...while loop, the "statement is always executed once before the condition is checked."1 Example: all.

r3. break without a label "terminates the innermost enclosing while , do-while , for , or switch immediately", and with a label "it terminates the specified labeled statement."1 continue "terminates the current iteration of the innermost enclosing while , do-while , or for statement and continues execution of the loop with the next iteration."1 Example: labels.

r4. for...of "executes a loop that operates on a sequence of values sourced from an iterable object."3 It "first calls the iterable's Symbol.iterator method, which returns an iterator, and then repeatedly calls the resulting iterator's next() method".3 So it works on arrays, strings, maps, sets and any user-defined iterable. If the loop exits early, "the return() method of the iterator is called to perform any cleanup."3

r5. for...in "iterates over all enumerable string properties of an object (ignoring properties keyed by symbols), including inherited enumerable properties."4 On an array it gives the indexes as strings, and also any other enumerable property: "for...in iterates over property names, for...of iterates over property values".1 Example: in-vs-of.

r6. A let declared in the head of a for loop gets a new binding for each pass. The specification copies the per-iteration bindings into a new environment with CreatePerIterationEnvironment.2 For for...of, "Each iteration creates a new variable."3 A function made in the loop body sees the value of its own pass, as the closures example shows. With var, all passes share one variable, because "variables declared with var are only function-scoped, but not lexically scoped".5

r7. Array methods such as forEach are not statements, and "There is no way to stop or break a forEach() loop other than by throwing an exception."6 MDN sends early exits to the statements: "Early termination may be accomplished with looping statements like for , for...of , and for...in ."6

Examples

Keys and values of the same array (rule r5).

const arr = [3, 5, 7];
arr.foo = "hello";
const keys = [];
for (const i in arr) keys.push(i);
const values = [];
for (const v of arr) values.push(v);
console.log(keys, values);

const obj = { foo: 1, bar: 2 };
for (const [key, val] of Object.entries(obj)) console.log(key, val);
Output
[ '0', '1', '2', 'foo' ] [ 3, 5, 7 ]
foo 1
bar 2

for...of with Object.entries gives the keys and values of a plain object, which is not iterable itself.

Leaving two loops at once with a label (rule r3).

const grid = [[1, 2], [3, 4], [5, 6]];
let found = null;
outer: for (const row of grid) {
  for (const cell of row) {
    if (cell % 2 === 0) continue;
    if (cell > 2) {
      found = cell;
      break outer;
    }
  }
}
console.log(found);
Output
3

One binding per pass (rule r6).

const fromLet = [];
for (let i = 0; i < 3; i++) fromLet.push(() => i);

const fromVar = [];
for (var j = 0; j < 3; j++) fromVar.push(() => j);

console.log(fromLet.map((f) => f()), fromVar.map((f) => f()));
Output
[ 0, 1, 2 ] [ 3, 3, 3 ]

Pitfalls

for...in over an array

for...in gives string keys, and it also visits added properties (rule r5). MDN: "it is better to use a traditional for loop with a numeric index when iterating over arrays".1

This code runs but gives a wrong answer on purpose.

const scores = [10, 20];
scores.label = "round 1";
let sum = 0;
for (const i in scores) sum += scores[i];
console.log(sum);

let total = 0;
for (const s of scores) total += s;
console.log(total);
Output
30round 1
30

return inside forEach

A return in the callback ends that call only, so the loop goes on (rule r7).

This code runs but gives a wrong answer on purpose.

const seen = [];
[1, 2, 3, 4].forEach((n) => {
  if (n === 2) return;
  seen.push(n);
});
console.log(seen);

const before = [];
for (const n of [1, 2, 3, 4]) {
  if (n === 2) break;
  before.push(n);
}
console.log(before);
Output
[ 1, 3, 4 ]
[ 1 ]

Comparison

"The JavaScript for loop is similar to the Java and C for loop."1 Python has no counting form: "Python’s for statement iterates over the items of any sequence (a list or a string), in the order that they appear in the sequence."7 That is the job of for...of in JavaScript (rule r4). Java's enhanced for needs an array or an Iterable: "The type of the Expression must be an array type (§10.1) or a subtype of the raw type Iterable , or a compile-time error occurs."8 Go has one loop keyword for all forms: "A "for" statement specifies repeated execution of a block. There are three forms: The iteration may be controlled by a single condition, a "for" clause, or a "range" clause."9 See Loop, For loop and While loop.

References

  1. MDN Web Docs, JavaScript Guide, Loops and iteration, the introduction; for statement; do...while statement; while statement; break statement; continue statement; for...in statement; for...of statement
  2. ECMAScript 2026 Language Specification (ECMA-262, 17th edition), clause 14, 14.7 Iteration Statements; 14.7.4.4 CreatePerIterationEnvironment; 14.7.5 The for-in, for-of, and for-await-of Statements
  3. MDN Web Docs, JavaScript Reference, for...of, the introduction; Description; Early exiting
  4. MDN Web Docs, JavaScript Reference, for...in, the introduction; Description
  5. MDN Web Docs, JavaScript Reference, for, Lexical declarations in the initialization block
  6. MDN Web Docs, JavaScript Reference, Array.prototype.forEach(), Description
  7. The Python Tutorial, Python 3.14, More Control Flow Tools, for Statements
  8. The Java Language Specification, Java SE 25 Edition, chapter 14, 14.14.2 The enhanced for statement
  9. The Go Programming Language Specification, For statements

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW3 standard
  • a history with a source