Understanding and flexible operation of "| |" and "&&" in Javascript-javascript

Source: Internet
Author: User

You must have seen this code: a = a| | " XXX ".

It is actually equivalent to the following three types of code:

A = a | | "XXX";

And:

if (!a) {   a = "xxx";}

And:

if (a = = NULL | | a = = "" "| | A = = undefined) {    a = "xxx";}

How to understand three kinds of code. You must first understand the question: what happens to data types in JavaScript when they are converted to bool types?

In JavaScript, data types can be classified as "true" and "false." That is, true when the truth is converted to the bool type; False if the value is converted to type bool. The following values are used for some common data types when converting to type bool:

Data type Value after conversion to bool
Null FALSE
Undefined FALSE
Object TRUE
function TRUE
0 FALSE
1 TRUE
Numbers outside of 0, 1 TRUE
String TRUE
"" (empty string) FALSE

In an If expression, JavaScript first converts the conditional expression to the bool type, and the expression is true if the logic in the if is executed, otherwise skipped.

Then take a look at the two JavaScript expressions "&&" and "| |". JavaScript is a type prophecy, so the two expressions in JavaScript may not be the same as in other languages.

The && algorithm is as follows:

If the value of the && left expression is true, the value of the right-hand expression is returned, otherwise the value of the left-hand expression is returned.

Other words:

var i = "" && "truth";//-i = ""; i = "truth" && "other Truth";//-i = "Other true value" I = "truth" && ""; -i = ""

|| the algorithm is as follows:

if | | The value of the left-hand expression is true, and the value of the right-hand expression is returned otherwise.

var i = "" | | " True value ";//-I =" truth "; i =" Truth "| | "Other truth";//-I = "truth" i = "truth" | | ""; -i = "truth"

So you can understand A = a | | "XXX"; Logic: If A is a false value (equal to null, an empty string "" ...). ), then "XXX" is assigned to A; Otherwise, assign the value of a to the a itself.

The following examples apply, using | | and && to streamline the program:

var parameter= "";  function Test (parameter) {      //return truth      return true;  }    Truth Operation  function operate1 (parameter) {      return "truth Operation";  }    False value operation  function operate2 (parameter) {      return "false value operation";  }    var result=test (parameter) &&operate1 (parameter);  Result=test (parameter) | | Operate2 (parameter);    Equivalent to  result=test (parameter) operate1 (parameter): operate2 (parameter);    alert (result); The truth Operation    //is also equivalent to  if (test (parameter)) {      result=operate1 (parameter);  } else{      result=operate2 (parameter);  }    Alert (Result)//truth operation  

JACK

@@ 12/11/2014 NJ USA

  

Understanding and flexible operation of "| |" and "&&" in Javascript-javascript

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.