Continuous && in JavaScript expressions and | | The difference _javascript skill of assigning value

Source: Internet
Author: User
The code is as follows:
Copy Code code 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 ('----------------------------------------------');
About ' | | '
_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);

Look at the output and you'll see:

The output of ' && ' is:

Copy Code code as follows:

4
4
0
You
0
False
23
1
Undefined
1
Null

The number of each row corresponds to each of the above test.

About ' | | ' The output results are:

Copy Code code as follows:

1
2
0
2
Hate
Ihateyou
True
Undefined
True
Null
True

If you look closely, you'll get a little clearer:

If there is no 0, false, undefined, null in multiple consecutive && expressions, it will get the value of the last "subexpression", otherwise the 0, false, undefined, NULL returns in the expression.

Multiple consecutive | | The expression will take the value of the first "subexpression", or 0, false, undefined, or null, and then remove the value of a "subexpression", and so on, until you find a "subexpression" that is not 0, false, undefined, NULL, and use it as the value of the entire expression.

Add:

The above does not seem to consider a situation, that is, there is a subexpression "How to do?" In fact, you can change the way you describe && | | Mode of work:

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

Iterate through each subexpression from left to right, and a Boolean cast of each subexpression, if a Boolean (subexpression) is false, the value of the entire expression is the value of this subexpression (0 or false or undefined or null or '), and the The subexpression of the face is no longer judged, and if all the Boolean (subexpression) is true, the value of the entire expression is the value of the last subexpression.

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

Iterate through each subexpression from left to right, and a Boolean cast of each subexpression, if a Boolean (subexpression) is true, the value of the entire expression is the value of the subexpression, and the subsequent subexpression is no longer "ignored"; if the Boolean (subexpression) is False, the Boolean of the latter subexpression is judged until a Boolean (subexpression) is found to be true, and if all Boolean (subexpression) is false, the value of the last subexpression is returned (0 or false or undefined or null or "").

Here to note:

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

Boolean (Undefined)!= boolean (' undefined '), which 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.