JavaScriptLoops, Arrays & Strings

Break & Continue

Learn how to control loop execution using the break and continue statements in JavaScript.

What is a Break Statement?Link to this section

A break statement is a control flow tool used to immediately exit out of a loop or a switch statement before it finishes its natural cycle. When JavaScript encounters a break keyword inside a loop, it stops all remaining iterations and jumps to the code directly following the loop block.

The loop stops completely when i reaches 3.

  • `for (let i = 1; i <= 5; i++)` is the loop trying to run 5 times
  • `if (i === 3)` checks if the counter has reached the target value
  • `break;` terminates the loop immediately
  • `console.log(i);` prints the current value of the counter

It is used to terminate a loop early when a specific condition or target is met, saving computing time.

Why Do We Need Break and Continue?Link to this section

We need loop control statements like break and continue to handle unexpected conditions without writing complicated loop boundaries.

  • Stop searching a database array once the user profile is found
  • Exit an infinite menu loop when the user chooses to quit
  • Skip processing invalid transactions in a financial report
  • Ignore weekend days when calculating work shifts
  • Stop adding items to a shopping cart once the maximum weight limit is reached
  • Skip empty rows when parsing an uploaded spreadsheet

Stops calculation immediately upon hitting bad data.

This example shows how a break prevents corrupt calculations when bad data appears in a list.

About Loop Control StatementsLink to this section

Break and continue act as remote controls for your loops, allowing you to alter the normal sequential flow. While break exits the loop entirely, continue only skips the current iteration and moves on to the next one. Both work with all major loop types including for, while, and do-while loops.

StatementActionWhere it works
breakExits the loop entirelyLoops and switch blocks
continueSkips current iterationLoops only

Notice that 2 is missing from the output because of continue.

Types of Loop Control StatementsLink to this section

1. The Break StatementLink to this section

The break statement completely exits out of the current loop or switch block. It is useful when your task is complete and you do not want to waste time checking the rest of the items.

Stops the loop as soon as i equals 4.

tip

Use break to optimize performance when searching large arrays so your code doesn't keep looping after finding the result.

warning

Putting a break statement outside of a loop or switch block will cause a syntax error.

2. The Continue StatementLink to this section

The continue statement skips the rest of the code inside the current loop iteration and immediately jumps to the next cycle. It is handy for ignoring specific items without breaking the whole loop.

The number 2 is skipped.

note

In a while loop, be careful where you update your counter when using continue, otherwise you might create an infinite loop.

3. Using Break in Nested LoopsLink to this section

By default, a break statement only exits the innermost loop it is placed inside. If you have loops inside loops, a break will only terminate the child loop.

Only the inner loop (j) is broken for each iteration of i.

warning

Do not assume a single break will exit both nested loops at once. It only exits the closest enclosing loop.

4. Using Labeled Statements with BreakLink to this section

JavaScript allows you to label loops with a custom name and a colon. You can then tell a break or continue statement to target that specific label, allowing you to break out of nested loops completely.

tip

Use labeled breaks sparingly as they can make code harder to read if overused.

Infinite Loops and BreakLink to this section

Sometimes loops are written without an end condition, relying entirely on an internal break statement to stop execution when a user or condition demands it.

warning

Always ensure your infinite loop has a reachable break condition, or your program will crash or freeze.

Continue in While LoopsLink to this section

Using continue inside a while loop requires care because condition increments might be skipped if placed after the continue statement.

warning

If you put n++ after continue in a while loop, n will never change once it hits that condition, causing an infinite loop.

Complete Coding ExampleLink to this section

Combines continue and break to audit a list of financial transactions safely.

This comprehensive program processes a list of transactions, skips zeros and negative numbers using continue, and stops completely upon hitting a security code using break.

Important NotesLink to this section

  • Break stops the entire loop immediately.
  • Continue skips only the current iteration of the loop.
  • Break can also be used to exit a switch statement.
  • A break statement outside of a loop or switch causes a syntax error.
  • Labels can be used with break and continue to target outer loops.
  • In while and do-while loops, be careful with increment placement when using continue.
  • Using break helps improve performance when searching through data arrays.
  • Continue is great for filtering out invalid data items during iteration.

SummaryLink to this section

Mastering break and continue gives you precise control over your loops. You can now terminate execution early or skip unwanted steps cleanly, preparing you for advanced data processing and problem-solving.

Check Your UnderstandingLink to this section

Javascript Break & Continue quiz
Question 1 of 6

What does the `break` statement do inside a loop?

Practice ChallengesLink to this section

1. Find the First Even Number

easy

Given an array of numbers [1, 3, 7, 4, 9, 12], write a loop that prints the first even number it finds and then uses break to stop searching.

Expected output

4

Hint: Check if a number modulo 2 is 0, print it, and break.

2. Stop at Word Stop

easy

Given an array of strings ["apple", "banana", "stop", "cherry"], loop through them and print each word. If the word is "stop", use break to terminate the loop.

Expected output

apple
banana

Hint: Compare the array element to the string "stop".

3. Skip Negative Scores

medium

Given an array of quiz marks [85, -1, 92, 45, -1, 78], write a loop that prints only the valid scores (greater than or equal to 0), using continue to skip the negative ones.

Expected output

85
92
45
78

Hint: If mark < 0 use continue.

4. Print Odd Numbers Only

medium

Write a loop from 1 to 10. Use continue to skip even numbers, and print only the odd numbers.

Expected output

1
3
5
7
9

Hint: If i % 2 === 0 use continue.

5. Nested Loop Break

hard

Write a nested loop where the outer loop runs from i = 1 to 3 and inner loop from j = 1 to 3. Use a labeled break to completely stop both loops when i = 2 and j = 2.

Expected output

1 1
1 2
1 3
2 1

Hint: Label the outer loop and call break outerLabel.

Rate this content

Sign in to rate

Discussion

Sign in to join the discussion