The difference between consecutive & amp; and | values in Javascript expressions

Source: Internet
Author: User

The Code is as follows:
Copy codeThe Code is as follows:
Function write (msg ){
For (var I = 0; I <arguments. length; I ++ ){
Document. write (arguments [I] + '<br/> ');
}
}
// About '&&'
Test1 = 1 & 2 & 3 & 4;
Test2 = '0' & 2 & 3 & 4;
Test3 = 1 & 2 & 0 & 4;
Test4 = 2 & 'I' & 'love' & 3 & 'you ';
Test5 = 'I' & 'hate' & 1 & 0 & 'you ';
Test6 = 1 & false & 'ihateyou' & '2 ';
Test7 = 2 & true & 'ihatehateyou' & '23 ';
Test8 = 4 & true & 'undefined' & 'true' & '1 ';
Test9 = 4 & true & undefined & 'true' & '1 ';
Test10 = 4 & true & 'null' & 'true' & '1 ';
Test11 = 4 & true & null & 'true' & '1 ';
Write (test1, test2, test3, test4, test5, test6, test7, test8, test9, test10, test11 );
Write ('----------------------------------------------');
// '|'
_ Test1 = 1 | 2 | 3 | 4;
_ Test2 = 0 | 2 | 3 | 4;
_ Test3 = 0 | '0' | 8 | 4;
_ Test4 = 2 | 'I' | 'love' | 0 | 'you ';
_ Test5 = 0 | 'hate' | 1 | 0 | 'you ';
_ Test6 = false | 0 | 'ihateyou' | '2 ';
_ Test7 = false | true | 'ihatehateyou' | '23 ';
_ Test8 = 0 | 0 | 'undefined' | 'true' | '1 ';
_ Test9 = 0 | 0 | undefined | 'true' | '1 ';
_ Test10 = 0 | false | 'null' | 'true' | '1 ';
_ Test11 = 0 | 0 | null | 'true' | '1 ';
Write (_ test1, _ test2, _ test3, _ test4, _ test5, _ test6, _ test7, _ test8, _ test9, _ test10, _ test11 );

Let's take a look at the output:

The output result of '&' is:

Copy codeThe Code is as follows:
4
4
0
You
0
False
23
1
Undefined
1
Null

The number of each row corresponds to each test above.

The output result of '|' is:

Copy codeThe Code is as follows:
1
2
0
2
Hate
Ihateyou
True
Undefined
True
Null
True

If you look at it carefully, you will be clear:

If no 0, false, undefined, or null exists in multiple consecutive & expressions, it obtains the value of the last "subexpression". Otherwise, it returns the values of 0, false, undefined, null returns.

Multiple consecutive | expressions take the value of the first "subexpression". If it is 0, false, undefined, if it is null, take the value of the next "subexpression", and so on until the "subexpression" that is not 0, false, undefined, or null is found ", and use it as the value of the entire expression.

Supplement:

The above does not seem to be taken into consideration, that is, what if there is a subexpression? In fact, you can use another way to describe & | the work method:

For (...)&&(...)&&(...)...

Traverse the child expressions from left to right, and forcibly convert each child expression into a Boolean value. If a Boolean (child expression) is false, then, the value of the entire expression is the value of this subexpression (0, false, undefined, null, or ''), and the subsequent subexpression is no longer judged; if all Boolean (subexpression) if all values are true, the value of the entire expression is the value of the last subexpression.

For (...) | (...)...

Traverse the child expressions from left to right, and forcibly convert each child expression into a Boolean value. If a Boolean (child expression) is true, then, the value of the entire expression is the value of this subexpression, and the subsequent subexpression does not "Ignore". If the Boolean (subexpression) is false, the Boolean condition of the next subexpression is determined, if the value of Boolean (subexpression) is true, if all values of Boolean (subexpression) are false, returns the value of the last subexpression (0, false, undefined, null, or '').

Note:

Boolean (false )! = Boolean ('false'), the former is false, and the latter is true.

Boolean (undefined )! = Boolean ('undefined'), the former is false, and the latter is true.

Boolean (null )! = Boolean ('null'), the former is false, and the latter is true.

Boolean (0 )! = Boolean ('0'), the former is false, and the latter is true.

Boolean ('') = false

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.