TypeScript Reference ยท Keywords and Declarations
TypeScript for, for...of and for...in loops
Reference for TypeScript loops: the classic for loop, for...of over iterables, for...in over keys (always strings), entries(), downlevelIteration and typing pitfalls.
TypeScript has the three JavaScript for loops, and each gives its loop variable a different type. The classic for (init; test; update) loop is for counters. for...of walks the values of any iterable (arrays, strings, Map, Set, generators) and types the variable as the element type. for...in walks the enumerable property names of an object and types the variable as string, which surprises many people.
for (let i = 0; i < 3; i++) {} for (const item of ["a", "b"]) {} // item: string for (const key in { x: 1, y: 2 }) {} // key: string
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Syntax
for (let i = 0; i < n; i++) { ... } for (const value of iterable) { ... } for (const [k, v] of map) { ... } for (const key in object) { ... } for await (const chunk of asyncIterable) { ... }
| Part | Description |
|---|---|
iterable |
Anything with [Symbol.iterator]. Arrays, strings, Map, Set, typed arrays and generators qualify. |
object |
Any object. Its own and inherited enumerable string keys are visited. |
| Loop variable | const or let; a type annotation is not allowed here (error TS2483). |
Errors and edge cases:
- Using
for...ofon a non-iterable is error TS2488 ("Type 'X' must have a 'Symbol.iterator' method that returns an iterator"). - With a
targetbelow ES2015, iterating anything but arrays needsdownlevelIteration; with the ES2024 target used here it just works. - The right side of
for...inmust be an object type,anyor a type parameter; astringor a union withundefinedis error TS2407.
Usage notes
- Prefer
for...offor values,entries()when you need the index, andObject.entries()overfor...infor objects. for...inincludes inherited keys and returns numeric-like keys first in ascending order; for arrays, the keys are strings such as"0".- Loop variables are per-iteration when declared with
letorconst, so closures capture the right value. - Narrowing and mutation. Changing the array while iterating can skip elements; iterate over a copy if needed.
for await...ofworks inside async functions and over async iterables; see async and await.- Performance. A plain indexed loop is fastest, but
for...ofis optimized in modern engines and almost always fast enough. noUncheckedIndexedAccessmakesscores[i]typenumber | undefined;for...ofavoids that.