Detailed explanation of JavaScript logic And operator _ javascript skills

Source: Internet
Author: User
This article mainly introduces the logic And operator in JavaScript. In JavaScript, the logic AND operator is expressed in JavaScript with a pair And sign (& amp, the logical AND operator is expressed by the double AND sign (&).

var bTrue = true;var bFalse = false;var bResult = bTrue && bFalse;

The following truth table describes the behavior of the logical AND operator:

It should be noted that the number of logical AND operations can be of any type, not only a Boolean value. If a certain number is not the original Boolean value, the logical AND operation does not necessarily return a Boolean value.

The operation behavior of the logical AND operator is as follows:

  • If one operation is an object and the other is a Boolean value, this object is returned.
  • If both operations are objects, the second object is returned.
  • If the number of operations is null, null is returned.
  • If the number of operations is NaN, NaN is returned.
  • An error occurs if the number of operations is undefined.
  • If both operations are of the boolean type, the return value is boolean.

Similar to the logic AND operation in Java, the logic AND operation in JavaScript is also a simple operation. That is, if the first operation determines the result, the second operation is no longer calculated. For logical AND operations, if the first operation is false, no matter what the value of the second operation is, The result cannot be equal to true.
Consider the following example:

Var bTrue = true; var bResult = (bTrue & bUnknown); // Error alert (bResult); // This row will not be executed

Code running result:

This code will cause an error when performing logical AND operations, because the variable bUnknown is undefined. The value of the variable bTrue is true, because the logic AND operation will continue to calculate the variable bUnknown. This will cause an error because the bUnknown value is undefined AND cannot be used for logical AND operations.
If you modify this example and set the first number to false, no error will occur:

Var bTrue = false; var bResult = (bTrue & bUnknown); // No error alert ("bTrue & bUnknown Result:" + (bResult )); // output "false"

In this Code, the script outputs the value returned by the logical AND operation, that is, the string "false ". Even if the value of the variable bUnknown is undefined, It is not calculated because the value of the first operation is false.

Running result:

Verify the operation behavior of the JavaScript logic And operator

Test code:

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.