Breaking Down break, continue, default, and switch in JavaScript

pushpesh kumarpushpesh kumar
3 min read

Control flow is at the heart of programming β€” and JavaScript offers several statements that give you fine-grained control over loops and conditionals. In this article, we’ll explore the break, continue, default, and switch keywords with examples to help you understand when and how to use them.


πŸšͺ break Statement

The break statement exits a loop or a switch block early, stopping further execution.

πŸ“Œ Use Case:

  • Exiting a for, while, or do...while loop when a certain condition is met.

  • Exiting from a switch case once the matching condition is handled.

βœ… Example: Exiting a Loop

for (let i = 1; i <= 5; i++) {
  if (i === 3) break;
  console.log(i); // prints 1 and 2
}

βœ… Example: In a switch

let fruit = 'apple';

switch (fruit) {
  case 'apple':
    console.log('It’s an apple');
    break; // exits the switch
  case 'banana':
    console.log('It’s a banana');
    break;
  default:
    console.log('Unknown fruit');
}

πŸ”„ continue Statement

The continue statement skips the current iteration of the loop and jumps to the next one.

πŸ“Œ Use Case:

  • You want to skip processing for some specific cases but keep the loop running.

βœ… Example:

for (let i = 1; i <= 5; i++) {
  if (i === 3) continue;
  console.log(i); // prints 1, 2, 4, 5 (skips 3)
}

🧠 switch Statement

The switch statement provides an elegant alternative to multiple if...else if...else blocks, especially when checking against discrete values.

πŸ“Œ Use Case:

  • Use when comparing one value against many possible constant matches.

βœ… Basic Structure:

switch (expression) {
  case value1:
    // code block
    break;
  case value2:
    // code block
    break;
  default:
    // default block
}

🧩 default in switch

The default keyword provides a fallback case in a switch block β€” similar to else in an if-else ladder.

βœ… Example:

let day = 'Wednesday';

switch (day) {
  case 'Monday':
    console.log('Start your week strong!');
    break;
  case 'Friday':
    console.log('Almost the weekend!');
    break;
  case 'Sunday':
    console.log('Rest and recharge!');
    break;
  default:
    console.log('Just another regular day.');
}

If no case matches, the default block executes.


πŸ” Summary Table

KeywordWhat It DoesTypical Usage Area
breakExits a loop or switch earlyfor, while, switch
continueSkips current loop iterationfor, while loops
switchCompares one value to multiple casesReplacing if-else ladders
defaultFallback block in a switch statementswitch only

🎯 Final Thoughts

Understanding break, continue, switch, and default helps you write cleaner, more readable, and efficient control flow in JavaScript. These small but powerful keywords often make a big difference in how your logic behaves β€” especially in loops and multi-branch decision structures.

0
Subscribe to my newsletter

Read articles from pushpesh kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

pushpesh kumar
pushpesh kumar