Do you really understand the logic operators of JavaScript?

Source: Internet
Author: User

(1) and operators (&&)
The execution process with the operator is as follows: javascript obtains each operand in sequence and converts them to a Boolean variable. If it is false, the value of this operand is directly returned (note, returns the original value before conversion, not necessarily a Boolean value), which interrupts the processing of the next operand. Otherwise, the next operand is processed. If the last operand still corresponds to the Boolean variable true, the value of the last operand is returned. The following Code demonstrates the execution principle:

VaR A = "1" & True & 0 & false & "ABC ";
Alert (a); // The value of A is 0.
VaR B = "ABC" & True & "123 ";
Alert (B); // The value of visible B is "123 ";

(2) or operator (|)
Similar to the operator, or the execution process of the operator is as follows: javascript obtains each operand in sequence and converts them to a Boolean variable. If it is true, the value of this operand is directly returned, interrupt the processing of the next operand; otherwise, continue to the next operand. If the last operand still corresponds to the Boolean variable false, the return value is returned. The following code demonstrates the execution principle:

VaR A = "ABC" ||" 123 ";
Alert (a); // The value of A is "ABC"
VaR B = false | "" | 0;
Alert (B); // The value of A is 0.

Or operator is often used in development.
(3) Non-operator (!)
Unlike the first two operators, non-operators always return Boolean variables. For example:

VaR A =! "ABC ";
Alert (a); // display the value of a as false
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.