JavaScript Switch Case

JavaScript switch case evaluates a switch condition, base on switch condition value matched case will run until break or end of case statement.

JavaScript Switch Case

Syntax

switch (condition){
    case value1:    
        // Do stuff if value1 is true
        [break;]
    case value2:
        // Do stuff if value2 is true
        [break;]
    ...
    ...
    case valueN:
        // Do stuff if valueN is true
        [break;]
    default:
        // Execute default if none of the case are execute
        [break;]
}

break keyword is optional for end of the case statement execute. break; is strongly recommended for preventing execute all other case statements.

Example: with break keyword

<script>
var weekname = "wednesday";
switch (weekname) {
    case "monday":
        document.writeln("Monday working day.");
        break;
    case "tuesday":
        document.writeln("Tuesday working day.");
        break;
    case "wednesday":
        document.writeln("Wednesday government holiday.");
        break;
    case "thursday":
        document.writeln("Thursday working day.");
        break;
    case "friday":
        document.writeln("Friday is a last working day of the week.");
        break;
    case "saturday":
        document.writeln("Saturday week end.");
        break;
    case "sunday":
        document.writeln("Sunday week end.");
        break;
    default:
        document.writeln("No any case found.");
        // last line end of the case statement, no need to write break; keyword 
}
</script>

Run it...   »

Example Result

JavaScript Switch statement (without break keyword)

Case 1 :

Without break; keyword matching case execute. Not only matching case but after all cases and default cases are execute.

Example: without break keyword

<script>
var weekname = "wednesday";
switch (weekname) {
    case "monday":
        document.writeln("Monday working day.");
    case "tuesday":
        document.writeln("Tuesday working day.");
    case "wednesday":
        document.writeln("Wednesday government holiday.");
    case "thursday":
        document.writeln("Thursday working day.");
    case "friday":
        document.writeln("Friday is a last working day of the week.");
    case "saturday":
        document.writeln("Saturday week end.");
    case "sunday":
        document.writeln("Sunday week end.");
    default:
        document.writeln("No any case found.");
}
</script>

Run it...   »

Example Result

Case 2:

In some case I am missing to write break; keyword. Switch case execute the matching Case (case "wednesday"_ also execute after cases until next break; keyword. No matter if cases value are not satisfy (Case "thursday") still cases are execute.

Example: execute until next break keyword

<script>
var weekname = "wednesday";
switch (weekname) {
    case "monday":
        document.writeln("Monday working day.");
        break;
    case "tuesday":
        document.writeln("Tuesday working day.");
        break;
    case "wednesday":
        document.writeln("Wednesday government holiday.");
    case "thursday":
        document.writeln("Thursday working day.");
        break;
    case "friday":
        document.writeln("Friday is a last working day of the week.");
        break;
    case "saturday":
        document.writeln("Saturday week end.");
        break;
    case "sunday":
        document.writeln("Sunday week end.");
        break;
    default:
        document.writeln("No any case found.");
}
</script>

Run it...   »

Example Result

JavaScript Switch statement (Check case range)

JavaScript switch case statement check cases range and execute the matched case otherwise execute default.

Example: execute until next break keyword

<script>
var bdate = 20;
switch (true) {
    case bdate >= 1 && bdate <= 10:     // Use Logical AND
        document.writeln("bdate between 1 to 10 date.");
        break;
    case bdate >= 11 && bdate <= 20:     // Use Logical AND
        document.writeln("bdate between 11 to 20 date.");
        break;
    case bdate >= 21 || bdate <= 30:     // Use Logical OR
        document.writeln("bdate between 21 to 30 date.");
        break;
    default:
        document.writeln("No any case found."); 
}
</script>

Run it...   »

Example Result