This article mainly introduces in detail the differences and connections between if and switch in javascript. It is very practical and practical. For more information, see. Today, when I changed the BoxScroll plug-in, because there were more than two conditions in the if statement, I immediately wanted to rewrite the switch. Halfway through, I suddenly remembered a requirement in code quality testing tools such as JSHint, which should be replaced with =, without the need for unreliable forced transformation. Then I suddenly guessed that changing to a switch would reduce the efficiency? Which of the following statements about the switch are =======?
With an idea, give me a chestnut and eat it with a better bite:
var a = '5';switch (a) { case 5: console.log('=='); break; case "5": console.log('==='); break; default:}
At last, the console displays ===, which can be safely used. I took my previous study notes. Well, in the third year of high school, I did say that the switch operator is a full-class operator, so no type conversion will occur. Here is a summary
1. if and switch
If is the most widely used, there is nothing to say. It is worth noting that if is actually similar to |, if conditionA in if (conditionA) {} else {} is true, after executing the code block before else, it will not read the code in else. And | when the preceding value is true, it will be ignored later, even if there are more errors. Based on this nature, of course, put the most commonly used code block in front to reduce the number of judgments. On the other hand, if there are a lot of if judgments, and the distribution of the number of possible executions is relatively even, then the subsequent judgment statements should execute the previous judgments one by one each time, which is not conducive to optimization. A good practice is to convert one-layer judgment statement into two-layer judgment statements, such
if (a > 0 && a <= 1) { //do something} else if (a > 1 && a <= 2) {} else if (a > 2 && a <= 3) {} else if (a > 3 && a <= 4) {} else if (a > 4 && a <= 5) {} else if (a > 5 && a <= 6) {}...
Change
if (a > 0 && a <= 4) { if (a <= 1) { //do something } else if (a > 1 && a <= 2) { } else if (a > 2 && a <= 3) { } else if (a > 3 && a <= 4) { }} else if (a > 4 && a <= 8) { //}..
Although each of the previous judgments has been added once, the subsequent judgments have been reduced (4-1) x n times, and are still fully earned. Suddenly, I think this method is a bit like nested loops. Placing a few loops outside will help to optimize the performance. It depends on the specific situation to divide the loop into two or more layers.
Switch is the most intimate comrade-in-arms of if. Whenever if you are too busy, you can come and build your hands. There is nothing to say about the switch and if mutual transfer estimation, and the switch and if are the same, they are all executed from top to bottom. The difference is that the else in the if statement does not work in the switch, it has its own younger brother: break. If the break is not met, the switch will continue to execute, as shown in figure
var a = 2;switch (a) { case 1: console.log("1"); //break miss case 2: console.log("2"); case 3: console.log("3"); default: console.log('no break');}
The console displays 2, 3, no break. In fact, it is quite understandable that break prompts the program to jump out of the internal execution body to the next case judgment. if no, it is equivalent to if (condition) {A} {B}, without else, of course, both A and B must be executed. There are also two small tip. One is that any expression can be written in the switch and case, such
switch (A + B) { case a * b: console.log("1"); break; case a / b + c: break; //... default: console.log('no break');}
In fact, the comparison is (A + B) === (a * B) and (A + B) === (a/B + c ). Second, switch has a special usage, such
switch (true) { case condition1: //do something break; case condition2: break; //... default: //.. ;}
In this case, each case in the switch is executed in order. Switch (false )? It is useless.
2. = and =
In one sentence, except that the operands are not converted before comparison, the equal and incomplete operators are no different from the equal and unequal operators.
Classic cases
var a = "5", b = 5;a == b //truea === b //falsevar a = "ABC", b = "AB" + "C";a === b //true
The reason why the following shows true is actually inseparable from the immutable string type. On the surface, B is just a simple concatenation of a string, but in fact it has nothing to do with the original B. Each string exists in a specific part of the memory pool. When B = "AB" + "C" is executed, the strings AB and C are destroyed, B points to the ABC location in the memory pool. Because the string ABC is found in the memory pool (because a references it, so it exists), B and a point to the same area, and all are equal. If no variable points to the string ABC before B, if the memory pool does not exist, a block is allocated to ABC and B points to ABC.
Two previous summaries are attached:
The above is all the content of this article. I hope you will like it.