In JavaScript if and switch,== and = = = Detailed

Source: Internet
Author: User

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 JS if judgment, and the number of possible execution of the distribution is more uniform, then the judgment of the next sentence each time to the front of a single 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 &am p;& a <= 3) {} else if (a > 3 && a <= 4) {} else if (a > 4 && a <= 5) {} else if (a > 5 && a <= 6) {} ...

  


Into

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 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:    console.log ("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",  b = 5;a = = b     //truea = = = B     //falsevar a = "ABC",  B = "AB" + "C"; a = = = 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.

In JavaScript if and switch,== and = = = Detailed

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.