Skip to content
Tutorials Center
My learning

Topics

TypeScript Reference ยท Keywords and Declarations

TypeScript while and do...while loops

Reference for while and do...while loops in TypeScript: syntax, when each runs, narrowing inside loops, infinite loops with break, and common off-by-one mistakes.

while repeats a block as long as its condition is truthy, testing before every iteration. do...while runs the block first and tests afterwards, so the body executes at least once.

Use these loops when you do not know in advance how many times to repeat: reading until a queue is empty, retrying until success, or stepping through a structure of unknown size. TypeScript applies the usual control-flow narrowing to the condition.

TypeScript
let n = 3;
while (n > 0) {
  n--;
}

Try it

TypeScript while with narrowing

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 do...while runs at least once

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 Infinite loop with an exit

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
while (condition) {
  body;
}

do {
  body;
} while (condition);
Part Description
condition Evaluated before each pass (while) or after each pass (do...while). Any value; truthiness decides.
body A statement or block. Use break to leave early and continue to jump to the next test.

Errors and edge cases:

  • do...while needs a semicolon after the closing parenthesis only by convention; automatic semicolon insertion adds it.
  • A condition that is a constant true or false is allowed by the compiler; linters warn.
  • Unreachable code after while (true) with no break is reported if allowUnreachableCode is false (TS7027).
  • A function with a declared return type that ends in while (true) without a break needs no final return: TypeScript knows the end is unreachable.

Usage notes

  • Check the update. The most common bug is a loop that never changes the condition, or changes it in the wrong direction.
  • Assignment in the condition (while ((x = next()) !== undefined)) is legal and narrows x, but wrap it in parentheses and compare explicitly for readability.
  • Narrowing in loops. A let variable that is assigned inside the loop body has its narrowing recomputed at the top of each iteration, so a check made before the loop may not hold. Use a const inside the body to keep a narrowed value.
  • for is clearer for counters. Use while when the stop condition is not a simple count. See for.
  • Async loops. await inside a while loop is fine in async functions and runs sequentially.
  • Labels let break and continue target an outer loop; see Labeled statements.
  • Safety valve. Add an iteration cap for loops that depend on external input.