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.
let n = 3; while (n > 0) { n--; }
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
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...whileneeds a semicolon after the closing parenthesis only by convention; automatic semicolon insertion adds it.- A condition that is a constant
trueorfalseis allowed by the compiler; linters warn. - Unreachable code after
while (true)with nobreakis reported ifallowUnreachableCodeisfalse(TS7027). - A function with a declared return type that ends in
while (true)without abreakneeds no finalreturn: 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 narrowsx, but wrap it in parentheses and compare explicitly for readability. - Narrowing in loops. A
letvariable 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 aconstinside the body to keep a narrowed value. foris clearer for counters. Usewhilewhen the stop condition is not a simple count. See for.- Async loops.
awaitinside awhileloop is fine in async functions and runs sequentially. - Labels let
breakandcontinuetarget an outer loop; see Labeled statements. - Safety valve. Add an iteration cap for loops that depend on external input.