JavaScript: switch case - select options
The reason is always one, the values are probably a lot. Or maybe vice versa: there will be several reasons for its appearance on one value. In any case, it is necessary to take only 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 there are usually several ways, and you need to make a choice.
The classical conditional operator if (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 = multipleof the condition values. A similar one can be obtained by using combinations of the If operator. However, undoubtedly, the first variant is syntactically and semantically more correct. In addition, formally the decision on the second option will require the developer of greater skill:
Here the combination if (...) {...} else {...} and try {...} cath (...) {...} provides the beginning of the AJAX exchange procedure in the algorithm. The response is processed in a similar set of conditional statements.
JavaScript switch case: example
The main external difference between the choice of option and one condition: you can write not two but several executable command blocks. Only one or only a few will be performed.
Construction JavaScript switch case allows you to provideseveral algorithms depending on the value of the variable or value of the expression. It is important to consider that the choice of the desired algorithm will be made by an exact match of the value of the condition in the switch () and the value that is indicated in the case. The symbol "1" and the digit 1 are not the same, type conversion is not performed here.
Executing Multiple Sections
A characteristic feature of the choices in the construction of JavaScript switch case is the absence of parenthesesblocks - {...}, which are entrusted to determine only the beginning and the end of the body of this operator. Each case section is terminated by a break statement, which may not exist.
In these examples, a break was skipped in the first sectioncase. The variant on the left will produce the result of Variant2, because it will start execution, and in the variant on the right, Variant1Variant2 will be received, since the first one will be executed, but there will be no effect from the second condition, and besides, the operators of the first section will not interfere, then second: between them there is no break statement, and twice the JavaScript switch case does not. It should be noted that changing the iCond variable in the body structure will not produce any effect.
The reality of the code and the reality of the problem
JavaScript is a great language, fast, concise, with a well-rounded and logical syntax. JavaScript construction switch case works great, it allows to clearly and accurately describe the desired variety of options from a particular condition.
Selection operators can be inserted into each other. In their sections, the case can be conditional statements, loops, any other constructs. Do not neglect the scope of variables. Used everywhere should be described globally.
However, you should not abuse JavaScript. Switch case is an excellent branching tool, but not mobile on large amounts of code. The more sophisticated and functional the choice of options is, the more difficult it will be to change later.
The real problem never stands still, and ifdo not take into account the changes in the process of its solution, then from the moment of completion of work, already in the first days of operation of the created resource, inaccuracies and defects will be evident. The task will undoubtedly go ahead, and the site will have to be finalized.
From this point of view, the main drawbackThe JavaScript switch case is derived from its size and complexity. If you do not abuse this, you can achieve the achievement done by compact and small choices.
Condition + solution = a new turn of movement
A correctly formulated problem approximates the solution. Although the task, by definition, itself can not decide anything and bring it closer.
The condition is used universally (privatea small task). Execution of the condition = execution of some code. Using JavaScript in a switch case is both a tribute to fashion and an obvious necessity. This is done in all languages, because it's simple and convenient.
Once there was born "Lisp" and "Prologue", similarthey are programming languages. There were battles over the operators of the transition and labels. Tried to get a decent rating of the functional style of writing programs. Today, a mass developer enthusiastically promotes object-oriented ideas.
However, in modern worksits essential meaning is conditional operators. But the essence of modern ideas is precisely that the objects themselves put conditions and find solutions. After all, an object is a collection of syntax and semantics.