JavaScript logical operator ' | | ' and "&&"

Source: Internet
Author: User

First, to say that | | (logical OR), which, literally, returns false only when both the front and back are false, otherwise it returns true.

Alert (true| | FALSE);    True
Alert (false| | true); True
Alert (true| | true); True
Alert (false| | FALSE); False

But, in the deep sense, there is another world, try the following code

Alert (0| | 1);

Obviously, we know that the previous 0 means false, and the next 1 means true, then the result above should be true, and the result of the fact returned is 1. Then look at the following code:

Alert (2| | 1);

We know that the previous 2 is true, and the back 1 is also true, so what is the return result? The test result is 2, continue to look:

Alert (' A ' | | 1);

Again, the front ' a ' is true, the back 1 is also true; the test result is ' a ', below

Alert (' | | | 1);

From the top, we know that the front "is false, the back 1 is true, and the return result is 1." Look at the bottom again.

Alert (' A ' | | 0);

The front ' a ' is true, and the back 0 is false, and the result is ' a ', continue below

Alert (' A ' | | ' B ');

The front ' a ' is true, the back ' B ' is false and the return result is ' a ', we continue below

Alert (' | | | 0);

The front "is false, and the back 0 is also false, and the result is 0

Alert (0| | ");

The previous 0 is false, the back "is false, and the return result is '

This means

1, as long as "| |" False in front, regardless of "| |" Followed by true or FALSE, return "| |" The value that follows.

2, as long as "| |" Preceded by true, regardless of "| |" Followed by true or FALSE, return "| |" The preceding value.

----------------------------------------------------------------------------------------------------------


Second, the following say && (logic and), literally, only true before and after the time to return true, otherwise return false.

alert (true&&false);    False
alert (true&&true); True
alert (false&&false); False
alert (false&&true); False

Then, based on the above experience, we look at the "&&" number, not just the case of Boolean type.

Alert (' &&1 ');

The knot is returned ', ' && ' front ' is false, followed by 1 is true.

Alert (' &&0 ');

The knot is returned ', ' && ' front ' is false, followed by 0 and false.

Alert (' a ' &&1);

The knot is returned 1, "&&" before "a" is true, followed by 1 is also true.

Alert (' a ' &&0);

The knot is returned 0, "&&" before "a" is true, followed by 0 is false.

Alert (' A ' && ');

The knot is returned ', ' && ' preceded ' A is true, followed by ' is false.

Alert (0&& ' a ');

The knot is returned 0, "&&" before ' 0 is false, followed by ' a ' is true.

Alert (0&& ');

The knot is returned 0, "&&" before "0 is false, followed by" is also false.

This means that

1, as long as "&&" is false, whether "&&" followed by true or false, the result will be "&&" the preceding value;

2, as long as "&&" is true, whether "&&" followed by true or false, the result will be returned to "&&" the value after;

Let's summarize:

1, as long as "| |" False before, regardless of ' | | ' Followed by true or FALSE, the result returns "| |" The value that follows.

2, as long as "| |" Preceded by true, regardless of "| |" Followed by true or FALSE, the result returns "| |" The preceding value.

3, as long as "&&" is false, whether "&&" followed by true or false, the result will be "&&" the preceding value;

4, as long as "&&" is true, whether "&&" followed by true or false, the result will be returned to "&&" the value after;


From the last two Tests, the logical operator, "| |" and "&&" are to follow the short-circuit principle, as long as the symbol before the true and false, can determine the return value.

It should be stated that the priority of "&&" is higher than "| |" , the following tests:

Alert (1| | ' A ' &&2);

The return result is 1.

Based on the principle of absurdity, we assume that "| |" The priority level is not less than "&&" (Here only so "no less" is to prove the same level of the same situation).

According to the conclusions we have drawn above (1), (1| | ' A ') will return the previous value 1, (1&&2) according to the conclusion (4) should return the following value 2. This is obviously not true, so the priority of "&&" is higher than "| |" Of

JavaScript logical operator ' | | ' and "&&"

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.