What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

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

You cannot use break to exit an Array.forEach() early in TypeScript. The callback is a separate function, so a break inside it cannot target the iteration. Use for...of for direct early exit, or choose a short-circuiting array method such as some() or find() when it better expresses the goal.

Why break does not work inside forEach()

break exits a loop or switch statement that contains it. A forEach() callback is its own function, not the body of a loop statement that break can exit. TypeScript follows the same JavaScript control-flow rules; changing the language or compiler does not make forEach() breakable.

MDN puts it plainly: “There is no way to stop or break a forEach() loop other than by throwing an exception.” MDN’s forEach() reference documents the method’s behavior, while its break reference explains where break is valid.

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

Use for...of when you need a direct early exit

A for...of loop gives you a loop body where break can stop iteration normally:

const values = [2, 4, 7, 8];

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

This logs 2 and 4, then exits when it reaches 7. Use a traditional for loop instead when you need explicit index control or custom increments.

Choose an array method when you want a search result

Array methods can stop once their particular condition is satisfied. Pick one based on the result your code needs:

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
Goal Method When it stops Result
Check whether any item meets a condition some() At the first callback result that is truthy A boolean
Check whether every item meets a condition every() At the first callback result that is falsey A boolean
Get the first matching item find() When it finds a match The matching element, or undefined
Get the first matching index findIndex() When it finds a match The index, or -1

Use some() for a boolean and side effects up to a stopping point

Return true from the callback when the condition is met; this stops further callbacks. Return false to continue:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
const found = values.some((value) => {
  if (value > 5) return true;
  console.log(value);
  return false;
});

Use find() to retrieve the match

If you want the first matching value rather than to perform work while iterating, use find():

const firstLargeValue = values.find((value) => value > 5);

For the matching position rather than the value, use findIndex(). Use every() when the question is whether all elements satisfy a condition and you want evaluation to stop at the first failure.

Why returning from the callback is not a break

A return inside a forEach() callback ends only that callback invocation. It does not prevent forEach() from invoking the callback for later elements. Also, forEach() itself returns undefined, so its return value cannot provide a search result.

Throwing an exception can terminate a forEach() operation, but it turns ordinary loop control into exceptional control flow. Prefer for...of or an appropriate short-circuiting method for routine early termination.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Handle asynchronous work with the right loop

forEach() does not wait for promises returned by an async callback. Consequently, forEach(async value => ...) does not give you sequential asynchronous processing or a reliable way to stop after an awaited condition.

For sequential work that may stop early, use for...of with await:

for (const value of values) {
  await processValue(value);
  if (shouldStop(value)) break;
}

When choosing between these patterns, use for...of for direct loop control and sequential await; use a short-circuiting array method when its boolean, element, or index result is what the caller needs. The MDN forEach() documentation covers its callback and promise behavior.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

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