Logic in the "JavaScript" JavaScript assignment statement with && and logic or | |

Source: Internet
Author: User

In other languages, we often see logical symbols appearing in judgment statements, such as

if (a| | b) {}

But in some JS-related questions or books, we sometimes see logic with && and logic or | | The symbol appears in an assignment statement or in a return statement, such as

var x=a| | b; Return a&&b| | C

The first time we saw it, we were probably confused, what's going on?

Because the expression is allowed to be assigned in JS, the resulting value is the result of the expression's operation. Such as

var a= 5;   var b= 6;   var c= A +b;  A= ten;  Console.log (c); // each  JS is performed sequentially, and subsequent assignments do not affect the result of an expression that was previously evaluated
var b= 6var c= a+var a= 5; Console.log (c); //
JS Although is in order, but in JS in the variable declaration will be processed in advance, the assignment operation will only be executed to the assignment statement, so execute to var c= a+b;

We know

Logic and && operation rules: Only true when the left and right are true, false on one side is false.

Logical OR | | Arithmetic rule: Only false if both left and right are false, and true when one is true.

So, in the assignment statement and the return statement, logical with && and logic or | | And what about it?

The value given and returned is not a Boolean value to be judged, but an operation result of an expression on either side of the operator.

For logic and &&:

When there is a false, returns the value on the false side;

When there are two false, the value before the operator (left) is returned;

When there is two true, the value after the operator (right) is returned.

Logic and && operations are short-circuit operations, and if one is false, the operation is stopped, and the value is returned as false. Such as

varA={}; varb=56; //WINDOW.AAA is a non-existent object,Console.log (window.aaa &&NULL);//undefinedConsole.log (NULL&& window.aaa);//NULLConsole.log (A &&NULL);//NULLConsole.log (window.aaa&& a);//undefinedConsole.log (a && b);// AboutConsole.log (b && a);//Object {}

to Logical OR | | , it's just the opposite of logic and &&:

When there is a true value, return true on one side;

When there are two true, returns the value of the operator before (left);

When there are two false, returns the value after the operator (right).

Logical OR | | Operation is also a short-circuit operation, in the left-to-right operation in the order of operations, only the first operand is false, the second operand, to return the value of the stop operation side, such as

 var  a={};   var  b=56;  console.log (window.aaa | |   null ); //   null  console.log (null  | | window.aaa); // window.aaa  console.log (A | |                null ); //      object {}  console.log (window.aaa| | a); //                   object {}  console.log (a | | b); //                   object {}  console.log (b | | a); // 56  

Logic in the "JavaScript" JavaScript assignment statement with && and logic or | |

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.