🔁 Taking Control of Loops in JavaScript

JavaScript Guide

Steering Loops with break and continue 🚗💨

JavaScript loops are kind of like this:

You do something…
“Do it again.”
“Again.”
“Again.”
“Okay, stop now.”

And to make sure we are the ones who can say “stop” or “skip this one”,
JavaScript gives us two special keys:

🛑 break → “This loop ends here.”

⏭️ continue → “Skip this round and move on.”

In this article, we’ll explore:

  • What they actually do
  • When you should never use them
  • Real-life scenarios where they save the day
  • And those “Why did JS do that?!” moments

All with lots of code, clear explanations, and plenty of humor 😎


🎡 First Things First: Uncontrolled Loops Are Dangerous

Thanks to loops, we can:

  • Iterate over lists
  • Check data
  • Make JavaScript do the same thing over and over again

But sometimes we want to say:

  • “I found what I was looking for, no need to continue”
  • “This element is nonsense, skip it”
  • “Everything after this point is none of my business”

And right here is where:

break and continue enter the stage 🎬


🛑 break: “Alright, That’s Enough. Everyone Go Home.”

break completely terminates the loop.
It doesn’t come back.
It doesn’t check in later.
It doesn’t even text you on WhatsApp.

🔹 The Most Basic break Example

for (let i = 1; i <= 10; i++) {
  if (i === 5) {
    break;
  }
  console.log(i);
}

🧠 What Happens Line by Line?

  • i = 1 → not 5 → print
  • i = 2 → print
  • i = 3 → print
  • i = 4 → print
  • i = 5 → condition met
  • breakthe loop is dead 🪦

📤 Output:

1
2
3
4

📌 Golden Rule:

The moment break runs, the loop will never run again.


🔍 Real-Life Scenario: Searching for Something

If you’re searching through a list and keep looping after you’ve found it,
something is definitely wrong 😄

const users = ["Ali", "Ayşe", "Cansu", "Mehmet", "Zeynep"];

for (let user of users) {
  if (user === "Cansu") {
    console.log("🎉 The person we’re looking for is found!");
    break;
  }
  console.log(user + " checked");
}

🧠 Logic Behind It:

  • Traverse the list one by one
  • When you find what you want:
    • Log a message
    • Exit with break
  • Don’t check the rest → better performance + cleaner logic ✨

⏭️ continue: “I Don’t Like This One, Skip It”

continue does not end the loop.
It only throws away the current iteration.

In other words:

“I’m ignoring this element, but I’m still moving forward.”


🔹 Example: Skip Even Numbers

for (let i = 1; i <= 10; i++) {
  if (i % 2 === 0) {
    continue;
  }
  console.log(i);
}

🧠 Step by Step:

  • i = 1 → odd → print
  • i = 2 → even → continue
  • i = 3 → print
  • i = 4continue

📤 Output:

1
3
5
7
9

📌 One-Sentence Summary:

continue → “Pretend this element never existed.”


🍔 Filtering Example: Real-Life Version

Imagine a list of food orders:

❌ cold
❌ stale
❌ incorrect

But:

🔥 hot
🔥 correct

const orders = ["cold", "hot", "stale", "hot"];

for (let order of orders) {
  if (order === "cold" || order === "stale") {
    continue;
  }
  console.log("This one is edible:", order);
}

🧠 Logic:

  • Skip what you don’t want using continue
  • Process only what’s useful

🔄 Be Careful with while Loops ⚠️

continue is more dangerous inside while loops.

❌ Incorrect Code (Infinite Loop)

let i = 0;

while (i < 5) {
  if (i === 2) {
    continue;
  }
  console.log(i);
  i++;
}

😱 What Happened?

When i === 2:

  • continue runs
  • i++ never runs
  • i stays at 2 forever
  • The loop never ends

✅ Correct Usage

let i = 0;

while (i < 5) {
  i++;
  if (i === 2) continue;
  console.log(i);
}

📌 Life-Saving Rule:

If you use continue,
your increment/decrement logic must come before it.


🧠 break vs continue — Clear Comparison

What Do You Want?Use
End the loop completelybreak
Skip just one iterationcontinue
You found what you were looking forbreak
Filter elements one by onecontinue
Improve performancebreak

💎 Pro Tips (Pure Gold)

1. Too many break / continue statements hurt readability

if (...) continue;
if (...) break;
if (...) continue;

→ Brain overload 🧠🔥

2. Sometimes array methods are cleaner

  • filter
  • find
  • some

But:

If a loop is necessary,
break and continue are tools of mastery.

3. break only exits the nearest loop

  • Be extra careful with nested loops 👀

🧩 Mini Cheat Sheet

  • 🔁 Loop = repetition
  • 🛑 break = end the loop
  • ⏭️ continue = skip this iteration
  • ⚠️ while + continue = think twice
  • 🧠 Less but correct usage = clean code

🎯 Final Words

break and continue are:

  • The brakes and turn signals of loops 🚦
  • Used correctly, they make your code faster
  • Used incorrectly, they send you straight to infinite loop hell 😈

Bir yanıt yazın

E-posta adresiniz yayınlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir