Skip to content
Tutorials Center
My learning

Topics

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.

TypeScript
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

TypeScript Types of the three loop variables

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.

TypeScript for...in keys are strings

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.

TypeScript for...in on an array is a mistake

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

TypeScript
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...of on a non-iterable is error TS2488 ("Type 'X' must have a 'Symbol.iterator' method that returns an iterator").
  • With a target below ES2015, iterating anything but arrays needs downlevelIteration; with the ES2024 target used here it just works.
  • The right side of for...in must be an object type, any or a type parameter; a string or a union with undefined is error TS2407.

Usage notes

  • Prefer for...of for values, entries() when you need the index, and Object.entries() over for...in for objects.
  • for...in includes 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 let or const, 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...of works inside async functions and over async iterables; see async and await.
  • Performance. A plain indexed loop is fastest, but for...of is optimized in modern engines and almost always fast enough.
  • noUncheckedIndexedAccess makes scores[i] type number | undefined; for...of avoids that.