Skip to main content

Switch case construct


The switch construction is used to compare a value for equality with different options.

In this case, equality is implied in the sense of the operator strict equality ===, it cannot compare with a regular expression or somehow else. That is, the values must be of the same type for equality to hold.


If the condition matches, then the code blockπŸ“Ÿ associated with the corresponding case is executed. If none of the conditions match, then the code specified in the default block, if any, is executed. To exit the construction, use the break command. If you do not specify it, the code block is automatically executed in the next case, etc. Therefore, we use break in our scripts, so as not to run the interpreter over all cases, thereby reducing the performance of the script.



A switch construct has one or more case blocks and an optional default block.

It looks like this:

switch (n) {
case 1:
// code block 1;
case 2:
// code block 2;
// .......
// other case options
// .......
// code block if none of the conditions match;

n - this is boolean condition.


Now you are on a stripped-down version of the site, after subscribing to Patreon, you will get full access to the training course, as well as access to our server's private channels in Discord.

Download our mobile application or get tested in our JavaScript telegram bot, and also subscribe to our news.

Become a Patron!

Sumerian school

Contributors ✨#

Thanks goes to these wonderful people (emoji key):

Philipp Dvinyaninov


Dmitriy Vasilev



πŸ› 🎨 πŸ–‹


πŸ–‹ πŸ› 🎨

Become a Patron!