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

Source: Internet
Author: User

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

?
12345678910111213 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) {}...

Into

?
12345678910111213 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

?
123456789101112 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

?
12345678910 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

?
1234567891011 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

?
1234567 var a = " 5 "     b = 5; a = = b     //true a = = = b     //false var a = "ABC" &NBSP;&NBSP; 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

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.