Today, when the plugin boxscroll, because if the condition of more than two, then immediately think of rewriting switch. Change to half, suddenly remember a jshint and other code quality testing tools, a requirement, with = = = to replace = =, without unreliable forced transformation. Then suddenly guess, change to switch will not reduce efficiency ah? The actual judgment in switch is = = or = = =?
With the idea, quickly raise a chestnut, a good bite to eat:
var a = ' 5 '; Switch (a) { case 5: console.log (' = = ') ; Break ; Case "5": console.log (' = = = '); Break ; default :}
The last console display is = = =, it seems to be safe to use. Looking for a previous study note, well, it is true that the switch judgment is a congruent operator , so the type conversion does not occur. Here's a recap.
1.if and switch
If is the most used, nothing much to say. One thing to note: If in fact and | | Much like, if Conditiona is true in the IF (Conditiona) {} else {}, then it does not look at the code in the else after it executes the code block before else. and | | When the front is true, the back is ignored, even if there are any more errors. Based on this nature, of course, the most likely to use the code block in front, reduce the number of judgments. On the other hand, if there are a lot of if judgments, and the number of possible execution of the distribution is more uniform, then the subsequent judgment statement each time to the previous judgment one after the execution, not conducive to optimization. It is better to turn a layer of judgment into a two-tier judgment statement, such as
if (a > 0 && a <= 1) {
//
do something }
else
if (A > 1 && a <= 2
else
if (A > 2 && a <= 3 Span style= "color: #0000ff;" >else if (A > 3 && a <= 4else if (A > 4 && a <= 5< Span style= "color: #000000;" ) {} else if (A > 5 && A & lt;= 6
Becomes
if (a > 0 && a <= 4) {
if (a <= 1
) {
//
do something }
else
if (a > 1 &am p;& a <= 2
else if (A > 2 && a <= 3 else< /span> if (A > 3 && a <= 4) {}} else if (A > 4 && a <= 8< Span style= "color: #000000;" ) { //}:
Although each of the previous judgments have been added once, but the subsequent judgment will be reduced (4-1) *n times, or full-earned. Suddenly feel this way and nested loops a bit like, the number of cycles in the outside to help optimize performance, how to split into two layers or even multi-layer depends on the specific situation.
Switch is if the most intimate comrade-in-arms, every time if you do not come over when you come to the handle. Switch and if reciprocal estimation there is nothing to say, and switch and if, like the order from the top down to perform the judgment, the difference is if the else in the switch does not work, it has its own brother: break. If you do not encounter Break,switch will continue to execute, such as
var a = 2; switch (a) { case 1 " 1 "); // break Miss case 2: Console.log ( "2" ); case 3: Console.log ( "3" ); default : Console.log ( ' No Break '
The last console shows 2,3,no break. In fact, it is good to understand that break is the prompt program to jump out of the internal execution to the next case judgment, if not, the equivalent of if (condition) {a}{b}, no Else,a and B of course to do. There are also two small tip, one is the switch and case can write any expression, such as
Switch (A + b) { case A * B: console.log ("1"); Break ; Case A/b + C: break; // ... default : console.log (' no Break ');}
The actual comparison is (a+b) = = = (a*b) and (a+b) = = = (a/b+c). The second is that switch has a special use, such as
Switch (true) { case condition1: //do something break; Case condition2: break; // ... default : // .. ;}
Each case in the switch is then judged in order. As for switch (false)? There are no eggs.
2.== and = = =
In a word, the congruent and non-congruent operators are no different from the equality and inequality operators except that the operands are not converted before comparison.
The most classic case
var a = "5", = 5= = b //truea = = = B //False
var a = "ABC", = "AB" + "C"= = = = B //true
The following reasons for displaying true are inseparable from the fact that the string type is immutable. The surface looks like B is simply stitching a string, but in fact it has no relationship with the original B. Each string has a specific place in the memory pool, and when b= "AB" + "C" is executed, the strings AB and C are destroyed, and B points to the location of the ABC in the Memory pool. Since the string ABC was found in the memory pool before the point (because a refers to it, so it exists), B points to the same area as a and equals to the same judgment. If none of the variables point to the string ABC before B, then the memory pool is not there, it will be in the same place to the ABC, and the B point to ABC.
Attached to the previous summary figure two photo:
JS if and switch,== and = = =