JavaScript

JS Basics

JS Variables & Operators

JS Data Types & Conversion

JS Numbers & Math

JS Strings

JS Dates

JS Arrays

JS Control Flow

JS Loops & Iteration

JS Functions

JS Objects

JS Classes & Modules

JS Async Programming

JS Advanced

JS HTML DOM

JS BOM (Browser Object Model)

JS Web APIs

JS AJAX

JS JSON

JS Graphics & Charts

JavaScript

JS Basics

JS Variables & Operators

JS Data Types & Conversion

JS Numbers & Math

JS Strings

JS Dates

JS Arrays

JS Control Flow

JS Loops & Iteration

JS Functions

JS Objects

JS Classes & Modules

JS Async Programming

JS Advanced

JS HTML DOM

JS BOM (Browser Object Model)

JS Web APIs

JS AJAX

JS JSON

JS Graphics & Charts

JS Break


📘 JavaScript breakExit Loops or Switch Blocks Immediately

The break statement terminates the loop or switch block where it appears. Once encountered, control jumps to the next line of code after the loop or switch.


🔹 Syntax

break;

🔹 Example 1 – Breaking a for Loop

for (let i = 1; i <= 5; i++) {
  if (i === 3) {
    break;
  }
  console.log(i);
}
// Output: 1 2
  • Loop stopped as soon as i === 3.


🔹 Example 2 – Breaking a while Loop

let i = 1;

while (i <= 5) {
  if (i === 4) {
    break;
  }
  console.log(i);
  i++;
}
// Output: 1 2 3

🔹 Example 3 – Inside switch Statement

let fruit = "banana";

switch (fruit) {
  case "apple":
    console.log("Apple selected");
    break;
  case "banana":
    console.log("Banana selected");
    break;
  default:
    console.log("Unknown fruit");
}
  • Without break, it would continue to execute all cases after a match (fall-through).


🔹 Breaking from Nested Loops

for (let i = 1; i <= 3; i++) {
  for (let j = 1; j <= 3; j++) {
    if (j === 2) break;
    console.log(`i=${i}, j=${j}`);
  }
}
  • Only breaks the inner loop, not the outer loop.


Practice Questions

Q1. What is the purpose of the break statement in JavaScript?

Q2. How does break affect the execution of a loop?

Q3. Write a for loop that prints numbers from 1 to 10 but stops at 6 using break.

Q4. Can you use break in both while and for loops? Explain with examples.

Q5. How does break work inside a switch statement?

Q6. What happens if you forget break in a switch case block?

Q7. Write a program that uses break to stop looping when a number divisible by 7 is found.

Q8. What is the difference between break and continue statements?

Q9. How can you use break in nested loops to stop only the inner loop?

Q10. Is it possible to break out of multiple nested loops with a single break? If not, what’s the alternative?


JS Break Quiz

Q1: What does the break statement do?

A. Skips current iteration
B. Stops loop execution
C. Restarts the loop
D. Continues to next line inside the loop

Q2: Where is the break statement commonly used?

A. if statements only
B. Inside objects
C. In loops and switch statements
D. Only inside functions

Q3: What happens if you remove break from a switch case?

A. Only matched case runs
B. All following cases execute
C. It throws an error
D. Skips default case

Q4: Which of the following loops can use break?

A. for
B. while
C. do...while
D. All of the above

Q5: Which statement is true about break in JavaScript?

A. Can be used to pause a loop
B. Can exit multiple loops at once
C. Exits the nearest enclosing loop
D. Only works in for loop

Q6: How to stop a switch case from falling through to the next case?

A. Use continue
B. Use stop
C. Use break
D. Use return

Go Back Top