# switch statement

The `switch` construct is used to select one value from some range of values.

## Syntax

`switch (variable) { case 'value1': /* a code located here will be executed if the variable has value1 */ break; case 'value2': /* a code located here will be executed if the variable has value2 */ break; case 'value3': /* a code located here will be executed if the variable has value3 */ break; default: /* a code located here will be executed if the variable has some other value */ break; };`

The comparison is done for strict equality. The `default` block is optional.

## Example

Let's display the user's language depending on the variable `lang` value:

`let lang = 'ru'; switch (lang) { case 'ru': alert('rus'); break; case 'en': alert('eng'); break; case 'de': alert('deu'); break; default: alert('language not supported'); break; };`

## Example

Let's display the year season, in which the value from the variable falls:

`let num = 3; switch (lang) { case 1: alert('spring'); break; case 2: alert('summer'); break; case 3: alert('autumn'); break; case 4: alert('winter'); break; };`

## See also

- the lesson from JavaScript tutorial
  detailing how to work with `switch-case`
- the `if` statement
  that also sets a condition

← →
↑
en ru