"JavaScript logic && with logic or" alert (true&& "a") do You know what pops up?

Source: Internet
Author: User

In JavaScript && arithmetic and | | Operations are used very often, but in some cases do you really understand what they mean?

For example: Today's analysis of the JQ AddClass method appears when this sentence

typeof Value = = = "string" && value

Just saw when really confused, found that the foundation is weak, but also what, immediately Baidu Ah, so found a good article:

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);    // truealert (false| | true);    // truealert (true| | true);        // truealert (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 (0| | " A ");

The same front 0 is false "a" is true. The test result is "a", continue to look:

Alert (0| | false)

Both sides are false and the test result is false, for | | Value to the right of the number.

After the above test is not difficult to find, the above code only see | | The value on the left of the number, if the left value is false, take the right value (whatever the right value is); The value to the left of the number is true? Take a look at the following tests:

Alert (2| | 1);

The previous is true followed by true, the test result is 2, then look at one:

Alert (2 | | 0)

The preceding 2 is true, followed by 0 is false and the result is 2.

This test feels so easy, of course I'm trying to illustrate something: or just look at | | The value to the left of the number, and if the value is true, take the left value (regardless of the value on the right). (PS: Right may not be the biological)

"JavaScript logic && with logic or" alert (true&& "a") do You know what pops up?

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.