JavaScript: switch case - select options

The reason is always the same; there are probably a lot of values. Or maybe the other way around: for one value there will be several reasons for its appearance. In any case, it is only necessary to make one decision, depending on a certain condition, or vice versa.

Even if the solution can have several options, only one path will lead to any goal, but usually there are several paths, and you need to make a choice.

JavaScript switch case

The classic conditional if statement (condition) {algorithm 1 if the condition is true} else {algorithm 2 if the condition is false} gives only two choices. But even a simple real task always gives a lot of options.

A simple β€œnot one” but one condition

JavaScript construct, switch case = multiple condition values. Similar can be obtained using combinations of the If operator. However, undoubtedly, the first option is syntactically and semantically more correct. In addition, formally, the solution according to the second option will require a higher qualification from the developer:

Complex if example

Here, the combination of if (...) {...} else {...} and try {...} cath (...) {...} provides the beginning of the AJAX exchange procedure in the algorithm. Response processing is performed by a similar set of conditional statements.

JavaScript switch case example

The main external difference between the choice of option from one condition: you can write not two, but several executable blocks of commands. Only one or only a few will be executed.

Simple option switch case

The construction of the J avaScript switch case allows several algorithms to be provided depending on the value of the variable or the value of the expression. It is important to consider that the choice of the necessary algorithm will be made by the exact coincidence of the value of the condition in switch () and the value that is indicated in case. The symbol "1" and the number 1 are not the same thing; type conversion is not performed here.

Performing multiple sections

A characteristic feature of the choices in the construction of J avaScript switch case is the absence of block brackets - {...}, which are entrusted with determining only the beginning and end of the body of this operator. Each case section ends with a break statement, which may not exist.

Running multiple case

In these examples, the break in the first section of the case is missing. The variant on the left will give the result of Variant2, because it will start execution from it, and in the variant on the right, Variant1Variant2 will be received, since the first will be executed, but there will be no effect from the second condition, and besides, nothing prevents the execution of the statements of the first section, then second: there is no break statement between them, and twice the JavaScript switch case does not. It should be noted that changing the iCond variable in the body of the construct will have no effect.

Code Reality and Task Reality

JavaScript is a great language, fast, concise with well-developed and logical syntax. The design of the JavaScript switch case works fine, allows you to clearly and accurately describe the desired variety of options from a specific condition.

JavaScript switch case example

Selection operators can be inserted into each other. In their sections, cases can be conditional statements, loops, any other constructs. The scope of variables should not be neglected. Used everywhere should be described globally.

However, JavaScript should not be abused. Switch case is a great branching tool, but not mobile on large amounts of code. The more thoughtful and functional the choice is made, the more difficult it will be to change it later.

The real problem never stands still, and if you do not take into account changes in the process of solving it, then from the moment of completion of work, already in the first days of operation of the created resource, inaccuracies and shortcomings will be obvious. The task will undoubtedly go ahead, and the site will have to be finalized.

From this point of view, the main drawback of the JavaScript switch case stems from its size and complexity. If you do not abuse it, you can ensure that compact and small choices are made.

Condition + decision = new round of movement

A correctly formulated problem brings the solution closer. Although the task, by definition, cannot solve and approximate anything by itself.

The condition is used universally (private small task). Fulfillment of a condition = execution of some code. Using a switch case in JavaScript is both a tribute to fashion and an obvious necessity. This is done in all languages, because it is simple and convenient.

JavaScript switch case multiple values

Once Lisp and Prologue were born, similar programming languages. There were battles over conversion operators and tags. I tried to get a decent rating of the functional style of writing programs. Today, a mass developer enthusiastically promotes object-oriented ideas.

However, until now, conditional operators find their essential value in modern works. But the essence of modern ideas lies precisely in the fact that objects set conditions for themselves and find solutions. After all, an object is a combination of syntax and semantics.


All Articles