JS in About | | and && as well! The understanding

Source: Internet
Author: User
Tags logical operators

In JavaScript logic operations, 0, "", null, False, undefined, Nan will be sentenced to false, the others are true;

In JavaScript, the first JS will be && and | | The values on both sides are converted to a Boolean type, and then the value,&& operation returns True if the following value is taken, if | | Returns true to take the preceding value,

Conversion rules:

object is true;

Non 0 digit is true;

The non-empty string is true;

others are false;

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;

logical operators, "| |" 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 "| |" Of

// if Foo exists, the value is not changed, otherwise the value of bar is assigned to Foo

Equals

if (! ODI) {    ODI= attr;}

Why if (!odi), why not write directly if (ODI)?
In fact, this is a more rigorous way of writing:! The function is to convert a variable of another type to the bool type.
The difference between the selected typeof 5 and typeof!5 can be tested, and the return is number and Boolean;

JS in About | | and && as well! The understanding

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.