Coding/Learn/javascript/break and continue
JavaScript Foundational Course
01.Introduction to JavaScript & V8 Engine
02.Console Output & Program Flow
03.Variables & Data Types (let, const, typeof)
04.Template Literals & String Interpolation
05.Type Coercion & Strict Equality (=== vs ==)
06.Arithmetic, Math Object & Modulo
07.Conditionals & If / Else Branching
08.Logical Operators & Nullish Coalescing (??)
09.Ternary Operator & Switch Statements
10.While & Do-While Loops
11.Standard For Loops & Iteration
12.Loop Control (Break & Continue)
13.Functions, Arrow Syntax & Return Values
14.Default Parameters, Rest & Spread Syntax
15.Arrays, Indexing & Mutating Methods
16.Array Iterators (map, filter, reduce)
17.Objects, Key-Value Pairs & Destructuring
18.Scope, Lexical Environment & Closures
19.Asynchronous JavaScript (Promises & Async/Await)
20.Capstone: Interactive E-Commerce Cart & Ledger Engine
Course Certificate0%

Complete all lessons to earn

LearnLoop Control (Break & Continue)
12 / 20 0%
Lesson 12 of 20
5 mins read

Loop Control (Break & Continue)

PreviousNext Lesson
In Plain English

The break statement immediately terminates loop execution and transfers control out of the loop. The continue statement skips the remainder of the current iteration and jumps directly to the next step.

The Mental Model

"continue is like skipping a song you dislike on a playlist and moving to the next track. break is pressing the stop button to turn off the music player completely."

Deep Dive: How It Works

break: Exits the nearest enclosing loop immediately.

continue: Bypasses remaining code inside the loop body for the current element and advances to the update expression.

Labeled Statements: You can label loops to break out of outer nested loops when needed.

Syntax Blueprint

if (skip) continue; ──▶ Next turn | if (done) break; ──▶ Exit loop

Use continue to skip unwanted items and break when search target is discovered.

PointCore Rules to Remember

Point star
continue: Skips the rest of current turn and proceeds to the next.
Point star
break: Completely stops and exits the loop.
Point star
Efficiency: Stops unnecessary CPU cycles once a target is found.

Common Beginner Traps & How to Fix Them

Mistake:Putting continue before the counter increment in a while loop.

Why it happens: Skipping the update step in a while loop traps you in an infinite loop.

How to fix: In while loops, ensure the counter increments before calling continue.

Pro Tip: Using continue to filter out invalid items early reduces indentation and makes loop logic cleaner.

Live Interactive Example

Hit Run Code to see it live
Filtering and Early Termination
JavaScript ES2024
1
2
3
4
5
6
7
8
9
10
Output Console
Click "Run Code" to view the rendered output.
How it works: continue skips 2,4,6,8 while break terminates the loop after 7.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Find first matching element and break

  • PointLoop through numbers 1 to 10.
  • PointIf i === 5, print "Found target: 5" and break.
1
2
3
4
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates