Skip to main content

Switch case construct

@serverSerrverlesskiy

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.

comparison

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.

Syntax#

Syntax

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;
break
case 2:
// code block 2;
break
// .......
// other case options
// .......
default:
// code block if none of the conditions match;
}

n - this is boolean condition.

Payment#

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

πŸ’΅

Resoner2005

πŸ› 🎨 πŸ–‹

Navernoss

πŸ–‹ πŸ› 🎨

Become a Patron!