Continuous && in JavaScript expressions | | The difference between the assignment value

Source: Internet
Author: User


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);

&& Output Results:


4
4
0
You
0
False
23
1
Undefined
1
Null

|| Output Result:


1
2
0
2
Hate
Ihateyou
True
Undefined
True
Null
True

A closer look will make it clearer:

Multiple consecutive && expressions if there is no 0, false, undefined, or null, it will get the value of the last "subexpression", otherwise the expression will be 0, false, undefined, and Null returned.

Multiple consecutive | | The expression will take the value of the first "subexpression", and if it is 0, false, undefined, NULL, the value of a "subexpression" is removed, 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 sub-expression "' What to do?" In fact, it can be described in a different way to describe && and | | Way of working:

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

Iterates through the individual sub-expressions from left to right, and each subexpression is coerced by a Boolean, and 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 "), after The sub-expression of the polygon is no longer judged; if all Boolean (subexpression) is true, the value of the entire expression is the value of the last sub-expression.

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

Each subexpression is traversed from left to right, and each subexpression is coerced by a Boolean, and if a Boolean (subexpression) is true, the value of the entire expression is the value of this subexpression, and the subsequent subexpression no longer "cares"; if a Boolean (subexpression) is False to determine the Boolean condition of the latter subexpression until a Boolean (subexpression) is found, or if all Boolean (subexpression) is false, returns the value of the last subexpression (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 '), 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

Continuous && in JavaScript expressions | | The difference between the assignment value

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.