JavaScript break Statement

Subject: JavaScript

The break statement in JavaScript is used to immediately exit from a loop or a switch block. When JavaScript encounters a break, it stops the current control flow and resumes execution from the statement following the block.


Syntax


Use Case 1: break in a Loop


Use Case 2: break in a while Loop


Use Case 3: break in a switch Statement

⚠️ If break is omitted from case 'B', the code will fall through and execute subsequent cases. This is usually not desired unless explicitly intended.


Use Case 4: Exiting Nested Loops with break


Key Takeaways

  • break is used to exit loops (for, while, do...while) or switch blocks.
  • Helps in stopping execution early based on a condition.
  • Always use break to prevent fall-through in switch statements.
  • Labeled break can terminate nested loops, which is useful for complex logic.
Next : JS Continue