Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PC×
Skip to content
World desk2 min

How to Break Out of a forEach Loop in TypeScript

TypeScript cannot break out of an Array.forEach() callback. Learn when to use for...of, some(), every(), find(), or findIndex() instead.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

You can’t use break to stop an Array.forEach() early. Its callback is a separate function, so the surrounding control flow cannot break out of it; TypeScript follows the same JavaScript behavior. Use for...of for a direct early exit, or choose a short-circuiting array method when you need a search or condition check.

Why break does not work inside forEach()

break applies to a loop or switch statement in its scope. A forEach() callback is a function, not the body of a loop statement you can break from. Putting break in that callback therefore produces an error rather than ending iteration. TypeScript does not change this JavaScript control-flow rule. See MDN’s break reference.

As an Amazon Associate I earn from qualifying purchases.

Returning from the callback is not a substitute: it ends only that callback invocation, and forEach() continues with later array elements. The MDN forEach() reference notes that throwing an exception is the only way to stop the method itself, but exceptions should not be used as routine loop control. Choose a construct designed for early termination instead.

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

Use for...of for a direct early exit

When the stopping condition is part of ordinary work in the loop, for...of is the clearest replacement. Here, iteration stops before logging the first value greater than 5:

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

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

Use a traditional for loop instead when you need explicit indexes or control over increments. Both provide a loop body where break applies directly.

Choose an array method when its result matches your goal

Array methods can stop once they have the answer their operation is designed to find. Prefer these over using a callback for arbitrary work and trying to interrupt it.

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

Use some() for a boolean answer

Return true when the condition is met; some() stops there. 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() or findIndex() to locate a match

If you want the matching value rather than a boolean, use find():

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

Use findIndex() when the index is what you need. Use every() when the question is whether all elements satisfy a condition and you want to stop at the first failure. These methods return useful answers; forEach() itself returns undefined.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Handle asynchronous work with an async-compatible loop

forEach() does not wait for promises returned by an async callback. As a result, forEach(async item => ...) does not make the callbacks run sequentially or let you stop the method by awaiting one callback. For sequential work that can stop, put await inside a for...of loop:

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

This waits for each processValue call before moving to the next value, and exits when shouldStop is true. If you intend concurrent work instead, use explicit promise composition rather than expecting forEach() to wait for its callbacks.

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

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

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.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.