Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

Put break; inside a loop when a condition means there is no reason to keep iterating. It exits the innermost enclosing loop, skips the rest of that iteration, and continues with the first statement after the loop. TypeScript uses JavaScript’s runtime control-flow rules, so adding a type annotation does not change what break does.

Stop a TypeScript loop when a condition is met

Write break; in the loop body, usually inside an if statement. For example, this for...of loop stops before it logs the first value greater than 10:

const values = [3, 7, 12, 18];

for (const value of values) {
  if (value > 10) {
    break;
  }
  console.log(value);
}

The output is 3 and 7. When value is 12, break exits the loop; console.log(value) is skipped for that iteration, and 18 is never visited. The TypeScript Handbook describes break as a way to exit a for statement: Variable Declaration.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Keep a value found before breaking

If you need the matching value after the loop, assign it before the break:

const names = ["Ada", "Grace", "Linus"];
let match: string | undefined;

for (const name of names) {
  if (name.startsWith("G")) {
    match = name;
    break;
  }
}

console.log(match); // "Grace"

The assignment happens before the loop exits, so match remains available afterward. The break prevents the loop from checking later names.

What does break exit in nested loops?

A plain break exits only the innermost enclosing loop. The outer loop resumes with its next iteration. If you need to leave multiple levels, one option is to label the outer loop and break to that label:

Rank #2
TypeScript Programming Language - Software Engineer & Coder T-Shirt
  • TypeScript implements a superset of syntax for strictly typed development, facilitating deep static analysis and enhanced development environment integration. The compiler translates source into standard script formats, ensuring parity across any runtime.
  • TypeScript is ideal for front-end developers, full-stack engineers, and software architects who build large-scale web applications. It serves those looking to improve code excellence, reduce bugs through static checking, and maintain complex projects more.
  • Lightweight, Classic fit, Double-needle sleeve and bottom hem
outer: for (const row of rows) {
  for (const value of row) {
    if (value === target) {
      break outer;
    }
  }
}

Alternatively, put the search in a function and use return when the result is found. That can make the exit path easier to follow when the nested search has its own purpose. A break label must identify an enclosing labeled statement; it is not a way to exit an unrelated loop.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Does break work in for...of?

Yes. A for...of loop is a loop, so break ends it in the same way it ends a classic for loop. The distinction is what each loop visits: for...of reads values from an iterable, while for...in iterates property keys. For an array, use for...of when the algorithm needs its elements; use for...in only when it specifically needs property names. See the TypeScript Handbook’s sections on Iterators and Generators and Variable Declaration.

For a for...of loop, emitted JavaScript can also depend on the compilation target and the downlevelIteration setting, particularly when supporting older JavaScript environments. Check the TypeScript reference for downlevelIteration if the generated code must support those targets. This is an output-compatibility consideration, not a change to the basic meaning of break.

Choose the loop form that fits the task

Loop form Use it when What break exits
Classic for You need an index or control over the iteration count. The current innermost loop.
for...of You need values from an iterable, such as array elements. The current innermost loop.
for...in You need an object’s property keys rather than array values. The current innermost loop.

TypeScript extends JavaScript with a type system; the types do not redefine JavaScript control flow. The TypeScript for JavaScript Programmers handbook explains that relationship.

Common break mistakes

  • Putting it outside an allowed construct: break belongs inside a loop or a switch; it cannot be used as a general way to leave a block or function.
  • Expecting an inner break to stop every loop: an unlabeled break exits only its innermost loop. Use an enclosing label or return from a function if the outer loop must end too.
  • Leaving statements after the break: statements later in the same iteration are skipped once the break runs.
  • Using for...in for array values: it iterates keys, not the elements the algorithm may expect.
  • Assuming unreachable statements will always be reported: TypeScript can report code after an unconditional break as unreachable. The TypeScript 1.8 release notes describe allowUnreachableCode as the option for disabling unreachable-code detection/reporting: TypeScript 1.8 release notes.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Using break with a using declaration

When a using variable belongs to a for statement, leaving that loop with break disposes the variable according to the TypeScript Handbook’s Variable Declaration guidance. This matters when the variable manages a resource whose cleanup is tied to its scope.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.