FSM Full Stack Masterclass
Platform under construction
JavaScript course badge

Control Flow

Intermediate

break & continue

break stops a loop or switch. continue skips the rest of the current loop iteration and moves to the next one.

for (const record of records) {
  if (!record.active) {
    continue;
  }

  if (record.id === targetId) {
    break;
  }
}

Control Flow

break and continue are flow controls, not shortcuts for messy loops.

break exits the nearest loop or switch immediately. It is useful when the work is complete, a match is found or a limit is reached.

continue skips the remaining statements in the current loop body and starts the next iteration. It is useful for ignoring invalid, inactive or irrelevant items early.

Both keywords can make loops clearer when used sparingly. If every few lines contain break or continue, the loop probably needs restructuring.

break

Stops the nearest loop or switch.

continue

Skips to the next loop iteration.

Early skip

Use continue near the top of a loop for invalid items.

Found match

Use break when continuing would waste work.

Examples

Control flow should make the program path easy to follow.

Skip invalid records, stop after match

let match = null;

for (const record of records) {
  if (!record.active) {
    continue;
  }

  if (record.id === targetId) {
    match = record;
    break;
  }
}

Control jumps hide the main path

for (const record of records) {
  if (record.active) {
    if (record.id === targetId) {
      match = record;
      break;
    }
  }
}

// The same behavior is harder to scan because the valid path is nested.

Rules that matter

Make branches, loops and exits deliberate.

Control flow decides which path code takes. Keep that path visible: name conditions, protect loop boundaries, avoid accidental fallthrough and use early exits only when they make the main path clearer.

Use continue to skip early

It keeps the valid path less indented.

Use break when the loop is done

Do not keep searching after the answer is found.

Avoid overusing jumps

Too many exits make flow hard to trace.

Remember nearest scope

break and continue affect the nearest loop, not every outer loop.

Use labels rarely

Labeled break exists, but most code is clearer with functions.

Prefer array methods for simple searches

find, some and every can communicate intent better than manual loops.

Production thinking

Reliable branching is reliable behavior.

Why does this matter?

Large loops become readable when irrelevant items are skipped and completed searches stop immediately.

Accessibility

Skipping invalid data can prevent broken labels, empty controls and incomplete messages from entering the interface.

Production note

Production loops should be efficient without hiding intent. break and continue should make the loop easier, not cleverer.

SEO note

When loops build links or content, continue can skip bad data and break can stop after a useful limit.

Live code lab

Change the HTML, CSS or JavaScript and run the result.

The preview runs inside an isolated iframe. The JavaScript is placed inside the HTML editor for now, so every example stays together and remains easy to understand.

Mini assignment

Try this now.

  • Change targetId to 2 and compare the log.
  • Remove continue and rewrite the loop with nested if blocks.
  • Remove break and see how much extra work the loop does.

Practice assignment

Do this before moving to the next topic.

  1. Write one loop that skips inactive records.
  2. Write one loop that stops after finding a match.
  3. Explain when Array.find would be clearer.

Try it yourself

Find the first active record

Live preview

Self-check

Before you continue, prove that you understand break & continue.

Intermediate

Read each question out loud and answer it without looking at the examples. Control flow only clicks when you can trace the path in your own words.

  1. Can you explain what break does?
  2. Can you explain what continue does?
  3. Can you explain how continue can reduce nesting?
  4. Can you explain why break can improve performance?
  5. Can you explain why too many jumps become confusing?

Chapter checkpoint

Control Flow checkpoint

Finish this chapter by turning the lessons into a small practical proof.

Build

Refactor a nested if/else example into guard clauses or a switch/lookup object.

Deliverables

  • original branch
  • refactored branch
  • short explanation

Quality checks

  • clear exit conditions
  • no unreachable branch
  • fallback/default path exists

Review question

Is the new control flow easier to read under pressure?