JavaScript Tutorial
JS - Introduction JS - Basic JS - Placement JS - Output JS - Statements JS - Syntax JS - Variables JS - Comments JS - Data Types JS - Operators JS - Operator Precedence JS - Condition JS - Switch JS - Functions JS - Objects JS - Loops For JS - Loops While JS - Break JS - Strings JS - String Methods JS - Numbers JS - Number Methods JS - Arrays JS - Array Methods JS - Array Iteration JS - Events JS - Event Listener JS - Event Propagation JS - Date JS - Math JS - Random JS - Boolean JS - Type Conversion JS - RegExp JS - Exception JS - Scope JS - Debugging JS - Hoisting JS - Strict Mode JS - this Keyword JS - Mistakes JS - Best Practices JS - Performance JS - Form Validation JS - ES6 Features
JS Objects
Object Definitions Object Properties Object Methods Object Constructors Object Prototypes
JS Functions
Function Definitions Function Parameters Function Call Function Apply Function Closures
JS HTML DOM
DOM Introduction DOM Methods DOM Selectors DOM HTML DOM CSS DOM Attributes DOM Navigation
JS Browser BOM
JS - Window JS - Screen JS - Location JS - History JS - Navigator JS - Popup Alert JS - Timing JS - Cookies
JS AJAX
AJAX - Introduction AJAX - XMLHttp AJAX - Request AJAX - Response AJAX - PHP
JS JSON
JSON - Introduction JSON - Syntax JSON - Data Types JSON - Parse JSON - Stringify JSON - Objects JSON - Arrays JSON - PHP JSON - JSONP
JS References
JavaScript Reference JavaScript Methods HTML DOM Reference

JavaScript Switch Case

Switch is a Conditional Statements same as if...else.., except a switch statement allows a variable to be tested for equality against a list of values.

Use switch to select one of many blocks of code to be executed. This is the perfect solution for long, nested if...else statements.

A switch statement looks as follows:

switch (expression) {
  case value1:
     //Statements executed when the
     //result of expression matches value1
     break;
  case value2:
     //Statements executed when the
     //result of expression matches value2
     break;
  ...
  case valueN:
     //Statements executed when the
     //result of expression matches valueN
     break;
  default:
     //Statements executed when none of
     //the values match the value of the expression
}

Following the logic of the code block above, this is the sequence of events that will take place:

  • The switch expression is evaluated once
  • The value of the expression is compared with the values of each case
  • If there is a match, the associated block of code is executed
  • If none of the cases match, the default block of code is executed

The following example uses the weekday number to calculate the weekday name:

Run code

The break Keyword

The break keyword associated with each case label ensures that the program breaks out of switch once the matched statement is executed and continues execution at the statement following switch. If break is omitted, the program continues execution at the next statement in the switch statement.

If you forget a break then the script will run from the case where the criterion is met and will run the case after that regardless if criterion was met:

Run code

It is not necessary to break the last case in a switch block. The block breaks (ends) there anyway.


The default Keyword

The default keyword specifies some code to run if there is no case match.

There can only be one default statement in a switch. Although this is optional, it is recommended that you use it, as it takes care of unexpected cases.


Multiple Cases

Sometimes you will want different cases to use the same code, or fall-through to a common default.

This is an example of a single operation sequential switch statement, where four different values perform exactly the same:

Run code

If multiple cases matches a case value, the first case is selected.

If no matching cases are found, the program continues to the default label.

If no default label is found, the program continues to the statements after the switch.