&& in JS | | Operators and IF statements

Source: Internet
Author: User

This kind of && | | Operators to write if statements are also referred to as short-circuiting in some populations. When I first learned this writing online, I saw an example:

var add_level = 0; if (Add_step = = 5) {add_level = 1;} else if (Add_step = =) {Add_level = 2;} else if (Add_step = =) {add_level = 3;} else if (Add_step = =) {add_level = 4;} else {add_level = 0;} This if Else statement is the longest-seeing and easiest-to-understand notation, but the drawback is that there is too much code. You can then use the switch statement:var add_level = 0;switch (add_step) {Case 5:add_level = 1;Break ;Case 10:add_level = 2;Break ;Case 12:add_level = 3;Break ;Case 15:add_level = 4;Break ;default:add_level = 0;Break ; In fact, the switch statement has been very streamlined, if the add_step==15, the "=" condition is changed to ">" This unequal condition, then the switch statement can not be achieved. Then let's look at &&, | | The notation of the operator: var add_level = (add_step==5 && 1) | | (add_step==10 && 2) | | (add_step==12 && 3) | | (add_step==15 && 4) | | 0;It's very streamlined and is implemented with just one line of code. This is the code, or the example that makes &&| | The operator has entered my eyeball. But also this example, let me understand this way of writing a lot of detours. After reading this example, I once thought that the pre-&& content is the judgment condition within the if (), followed by the execution code after the condition is established, and | | Represents the else{}. This is a big mistake, let me tell you,&&, | | The nature of the operator: take the following code to say var Add_level = (add_step==5 && 1) | | (add_step==10 && 2) | | (add_step==12 && 3) | | (add_step==15 && 4) | | 0; This is actually the "and" or "judgment." First judge from left to right, if the add_step==5 run result is true, that is true&&1, the result is 1, is judged true (but because it is assigned as a number to the variable, so it is judged true, but it will not be converted to true). And the back of the next is | | operator, the current face is judged to be true and no further operations are performed backwards. So the final result is 1, which is then assigned to Add_step. And if the add_step==5 operation results in false, then add_step==5&&1 the whole is false, then the following | | The operator continues the backward operation, looking behind. This gives a person an illusion similar to the if () {}else{} statement. In fact, these two kinds of writing, only the final result is the same, but the principle of operation is really different. operator notation, although very concise, refined. But the logic involved is very complex. Generally do not use a lot of development, because it is likely to over a period of time, you are in a short period of time you can not understand, not to mention let others to maintain. But the following code is the exception. Often used, very convenient and effective, can be a good solution to the problem of naming repetition.

var YAHOO = window. YAHOO | | {};

This is the Yahoo website processing JS namespace statement. This writing, at present in many websites is also more popular and frequent occurrence.

Yahoo is a variable,| | The relational operator is equivalent to an if statement.

If window. Yahoo is already defined, then yahoo = window. YAHOO, otherwise yahoo={};

{} is a shorthand for new Object ().

var YAHOO = {}; is to declare an "object-level variable " to YAHOO.

&& in JS | | Operators and IF statements

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.