JavaScript Switch Statement

The switch statement selects one of many code blocks to execute. It is a cleaner alternative to many else if chains when comparing one value against many cases.

Syntax

Switch — Days of the Week
let day = 3;
switch (day) {
    case 1: console.log("Monday");    break;
    case 2: console.log("Tuesday");   break;
    case 3: console.log("Wednesday"); break;
    case 4: console.log("Thursday");  break;
    case 5: console.log("Friday");    break;
    default: console.log("Weekend");
}

The break Keyword

Without break, execution falls through to the next case — even if the value doesn't match.

Fall-through Without break
let x = 2;
switch (x) {
    case 1:
        console.log("Case 1");
        // no break — falls through!
    case 2:
        console.log("Case 2");
        // no break — falls through!
    case 3:
        console.log("Case 3");
        break;
}
// Prints both "Case 2" and "Case 3"

The default Keyword

The default case runs when no other case matches. It is optional.

Default Case
let fruit = "mango";
switch (fruit) {
    case "apple":  console.log("Apple");  break;
    case "banana": console.log("Banana"); break;
    case "cherry": console.log("Cherry"); break;
    default:       console.log("Unknown fruit: " + fruit);
}

Multiple Cases — Same Block

Grouped Cases
let day = 6;
switch (day) {
    case 1:
    case 2:
    case 3:
    case 4:
    case 5:
        console.log("Weekday"); break;
    case 6:
    case 7:
        console.log("Weekend"); break;
}

Strict Comparison

Switch uses strict equality (===). A string "5" will NOT match a number 5.

Strict Comparison in Switch
let val = 5;
switch (val) {
    case "5": console.log("String 5"); break;  // won't match
    case 5:   console.log("Number 5"); break;  // matches!
}
📝 Note: Always add a break to each case unless you intentionally want fall-through. Missing breaks are one of the most common JavaScript bugs.
Exercise:
What keyword stops execution from falling through to the next case in a switch?
Try it YourselfCtrl+Enter to run
Click Run to see the output here.