|
| 1 | +# About |
| 2 | + |
| 3 | +## General Syntax |
| 4 | + |
| 5 | +Besides the if-statement, JavaScript also has a switch-statement to conditionally execute logic. |
| 6 | +It is used when a single variable needs to be compared to multiple variants. |
| 7 | +The comparison is done by checking for strict equality (`===`), see [concept comparison][concept-comparison]. |
| 8 | +For some variable `x`, the switch statement in JavaScript has the following syntax. |
| 9 | + |
| 10 | +<!-- prettier-ignore-start --> |
| 11 | +```javascript |
| 12 | +switch (x) { |
| 13 | + case option1: |
| 14 | + // code that is executed when "x === option1" is true |
| 15 | + break; |
| 16 | + case option2: |
| 17 | + // code that is executed when "x === option2" is true |
| 18 | + break; |
| 19 | + default: |
| 20 | + // code that is executed when x does not equal any of the options |
| 21 | +} |
| 22 | +``` |
| 23 | +<!-- prettier-ignore-end --> |
| 24 | + |
| 25 | +The `default` case is optional and used in case you want to execute some code if none of the other options match the variable. |
| 26 | + |
| 27 | +## Fallthrough by Default |
| 28 | + |
| 29 | +The `break` statements above are needed because by default all cases are "fallthrough" in JavaScript. |
| 30 | +That means without any `break` statement all the code in the cases below the first matching option would be executed even though `x` did not match those options. |
| 31 | +This "fallthrough by default" behavior is a common pitfall when using `switch` in JavaScript. |
| 32 | +Inside a function, `return` can also be used instead of `break` to avoid this problem. |
| 33 | + |
| 34 | +You can use the fallthrough behavior to your advantage when you want to apply the same code for multiple cases. |
| 35 | +You can find an example of this in the [MDN documentation][mdn-group-cases]. |
| 36 | + |
| 37 | +## Scope |
| 38 | + |
| 39 | +By default the variables in the different `case` statements share the same scope. |
| 40 | +This can lead to unexpected behavior. |
| 41 | +For example due to copying and pasting a case, you could end up with a `let message` declaration in two cases which results in an error, see [MDN documentation][mdn-switch-scope]. |
| 42 | +To avoid problems due to the shared scope, you can create a separate scope for each case statement by adding code blocks with curly brackets for each case. |
| 43 | + |
| 44 | +```javascript |
| 45 | +switch (x) { |
| 46 | + case option1: { |
| 47 | + // Variables declared here are contained to this case. |
| 48 | + break; |
| 49 | + } |
| 50 | + case option2: { |
| 51 | + // ... |
| 52 | + break; |
| 53 | + } |
| 54 | + default: { |
| 55 | + // ... |
| 56 | + } |
| 57 | +} |
| 58 | +``` |
| 59 | + |
| 60 | +## Using Expressions |
| 61 | + |
| 62 | +Instead of a variable `x`, you can also use an expression. |
| 63 | +That expression is evaluated once at the beginning of the switch statement and the result compared against the cases. |
| 64 | +A common use of this is a "type switch" that executes different code depending on the type of a variable. |
| 65 | + |
| 66 | +<!-- prettier-ignore-start --> |
| 67 | +```javascript |
| 68 | +switch (typeof x) { |
| 69 | + case 'string': |
| 70 | + // code that is executed when x is a string |
| 71 | + break; |
| 72 | + case 'number': |
| 73 | + // code that is executed when x is a number |
| 74 | + break; |
| 75 | + default: |
| 76 | + // code that is executed when x has some other type |
| 77 | +} |
| 78 | +``` |
| 79 | +<!-- prettier-ignore-end --> |
| 80 | + |
| 81 | +The options can be expressions as well. |
| 82 | + |
| 83 | +[concept-comparison]: /tracks/javascript/concepts/comparison |
| 84 | +[mdn-group-cases]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch#methods_for_multi-criteria_case |
| 85 | +[mdn-switch-scope]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch#block-scope_variables_within_switch_statements |
0 commit comments