JavaScript Tutorial

JavaScript Break and Continue

break leaves the loop now. continue skips the rest of this round and starts the next one.

Two ways to change a loop

A loop normally runs until its test is false. Sometimes you want to stop early. Sometimes you want to skip one round and keep going. That is what break andcontinue are for.

Both work in for, while, and do...while. They also work in switchbreak there leaves the switch, not a loop.

break leaves now

When the engine hits break, it jumps to the first statement after the loop. The remaining rounds never run. Use it when you already have the answer: a match, a stop value, a found item.

Example

const scores = [12, 18, 9, 21, 7];
let found = null;

for (let i = 0; i < scores.length; i++) {
  if (scores[i] > 20) {
    found = scores[i];
    break;
  }
}

console.log(found);

The loop would have checked 7 as well. After 21 it stops, sofound is 21 and the last item is never read.

Click Try it in JavaScript under an example. That opens/javascript/try — a live page and a console.

continue skips this round

continue does not leave the loop. It jumps to the next round: the step in afor, or the test in a while. Code after continue in that same round does not run.

Example

const nums = [1, 2, 3, 4, 5, 6];
const odds = [];

for (const n of nums) {
  if (n % 2 === 0) {
    continue;
  }
  odds.push(n);
}

console.log(odds);

Even numbers hit continue and skip push. The result is[1, 3, 5]. A filter would do the same job; continue is useful when the body is longer than one line.

break versus continue

breakcontinue
Leaves the loopYesNo
Skips the rest of this roundYes — and every later roundYes — only this round
Typical useStop when you have enoughSkip a bad or unneeded item

If you mix them up, you either stop too soon or keep looping after you already have the result. Read the test next to the keyword: “leave now” or “try the next one.”

while and an early stop

A while with break is a search that can finish the moment it succeeds. Without break you would keep walking after the match.

Example

const names = ["Ada", "Lin", "Grace", "Alan"];
let i = 0;
let match = null;

while (i < names.length) {
  if (names[i] === "Grace") {
    match = names[i];
    break;
  }
  i += 1;
}

console.log(match);
console.log("Stopped at index " + i);

Nested loops

break and continue apply to the innermost loop that contains them. Breaking the inner loop does not stop the outer one. Labels exist for breaking an outer loop, but you rarely need them at this stage — a flag or a functionreturn is clearer.

Example

const grid = [
  [1, 2, 3],
  [4, 9, 6],
  [7, 8, 0],
];

for (let r = 0; r < grid.length; r++) {
  for (let c = 0; c < grid[r].length; c++) {
    if (grid[r][c] === 9) {
      console.log("found at", r, c);
      break;
    }
  }
}

After printing found at 1 1, the inner loop stops. The outer loop still runs row 2. If you need to stop both, put the search in a function andreturn from it.

What to remember

  • break exits the loop immediately.
  • continue skips the rest of the current round.
  • Both affect the innermost loop unless you use a label.
  • In a switch, break ends the switch, not a surrounding loop.

Next: Sets — a collection that throws duplicates away so you do not need a loop to unique a list by hand.

FAQ: JavaScript Break and Continue

Common questions about this page.

What is the StudyGrid JavaScript tutorial?

The StudyGrid JavaScript tutorial is a full beginner track: variables, functions, arrays, if/else, the DOM, events, storage, and fetch. Each chapter has copy-and-run examples.

Should I run javascript break continue examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn javascript break continue in this JavaScript JavaScript lesson (JavaScript Break and Continue).

Is the JavaScript editor the same as Try Python?

No. Try JavaScript is a live page plus a console at /javascript/try. Try Python stays at /try. JavaScript lessons never open the Python editor.

Do I need to install anything to learn JavaScript?

No. Open a chapter, click Try it in JavaScript, and the page and console update in the browser.

Where should I start the JavaScript tutorial?

Start at JavaScript Intro, then Where To, Output, and Syntax. After variables and functions, continue to the DOM and events. Use Next at the bottom of each chapter.

Is the JavaScript tutorial free?

Yes. The JavaScript studio on StudyGrid (studygrid.in) is free: dashboard, chapters, and the live editor.