Your product could be here, in front of thousands of JavaScript developers. Sponsor this spot →
JavaScript Quiz

What happens if you forget break in a JavaScript switch?

Do you know what will be the output of this code?

JavaScript
const n = 1;
switch (n) {
  case 1:
    console.log("one");
  case 2:
    console.log("two");
    break;
  default:
    console.log("other");
}
Show answer and explanation

The answer is one, then two.

case 1 matches, but there’s no break, so execution falls through into case 2 and logs "two". The break there stops it before default.

Take the full quiz Next question →

More Basics questions

  1. What happens when you reassign a const in JavaScript?
  2. Why is typeof null "object" in JavaScript?
  3. What is the difference between == and === in JavaScript?
  4. What does Array.push() return in JavaScript?
  5. Is a var loop variable accessible after a for loop?

Related topics