JavaScript-logic And Operator details

Source: Internet
Author: User

In JavaScript, the logical AND operator represents 1 var bTrue = true with the Ampersand (&); 2 var bFalse = false; 3 var bResult = bTrue & bFalse; the following truth table describes the logic AND operator behavior: Operation count 1 operation count 2 result true truetrue false falsefalse true falsefalse false it must be noted that: the number of logical AND operations can be any type, not just Boolean values. If a certain number is not the original Boolean value, logical AND operations do not necessarily return the operation behavior of the Boolean value logical AND operator as follows: if one operation number 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 boolean, the return value is similar to the logical AND operation in Java. The logical 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: 1 var bTrue = true; 2 var bResult = (bTrue & bUnknown); // error 3 alert (bResult ); // This line does not execute the code running result: This line of code will cause an error during the logic AND operation, 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, the following error will not occur: 1 var bTrue = false; 2 var bResult = (bTrue & bUnknown ); // No error occurs. 3. 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: Test code for verifying the operation behavior of the JavaScript logic And operator: Copy code 1 <script type = "text/javascript"> 2 document. write ("verify the operation behavior of the JavaScript logic And operator:"); 3 document. write ("<br/>"); 4 document. write ("-----------------------------------------------------------------------------"); 5 document. write ("<br/>"); 6 7 var bTrue = true; 8 var bFalse = false; 9 var bResult = bTrue & bFalse; 10/* 1. if both operations are boolean, the return value is boolean */11 docu. Ment. write ("1. if both operations are of the boolean type, the return value is "); 12 document. write ("<br/>"); 13 document. write ("-----------------------------------------------------------------------------"); 14 document. write ("<br/>"); 15 document. write ("bTrue = true, bFalse = false, the result of bTrue & bFalse is:" + (bResult); // The result is false16 document. write ("<br/>"); 17 18 document. write ("------------------------------------------------------------- ---------------- "); 19 document. write ("<br/>"); 20 21 var obj = new Object (); 22/* 2. if one operation is an object and the other is a Boolean value, this object */23 document is returned. write ("2. if one operation is an object and the other is a Boolean value, this object is returned "); 24 document. write ("<br/>"); 25 document. write ("-----------------------------------------------------------------------------"); 26 document. write ("<br/>"); 27 document. write ("obj is an object. The result of true & obj is:" + (true & obj); 28 document. write ("< Br/> "); 29 document. write ("obj = true & obj:" + (obj = (true & obj); 30 document. write ("<br/>"); 31 document. write ("false & obj:" + (false & obj); 32 document. write ("<br/>"); 33 34 document. write ("-----------------------------------------------------------------------------"); 35 document. write ("<br/>"); 36/* 3. if both operations are objects, the second object is returned. */37 var obj1 = new Object (); 38 var obj2 = new Object (); 39 document. write ("3. If both operations are objects, the second Object is returned. "); 40 document. write ("<br/>"); 41 document. write ("-----------------------------------------------------------------------------"); 42 document. write ("<br/>"); 43 document. write ("obj1 is an object, and obj2 is an object. The result of obj1 = (obj1 & obj2) is: "+ (obj1 = (obj1 & obj2); // The result is false44 document. write ("<br/>"); 45 document. write ("obj1 is an object, and obj2 is an object. The result of obj2 = (obj1 & obj2) is: "+ (obj2 = (obj1 & obj2); // The result is true46 document. write ("<br/> "); 47 48 document. write ("plaintext"); 49 document. write ("<br/>"); 50/* 4. If the number of operations is null, null is returned. */51 var a = null; 52 var B = true; 53 document. write ("4. If the number of operations is null, null is returned. "); 54 document. write ("<br/>"); 55 document. write ("a = null, B = true, the result of a & B is:" + (a & B); 56 document. write ("<br/>"); 57 document. write ("-----------------------------------------------------------------------------"); 58 document. write ("<br/>"); 59/* 5. if the number of operations is NaN, NaN */60 var c = NaN; 61 var d = "str"; 62 document. write ("5. if the number of operations is NaN, NaN "); 63 document. write ("<br/>"); 64 document. write ("c = NaN, d = str, c & d: "+ (C & d); 65 document. write ("<br/>"); 66 document. write ("-----------------------------------------------------------------------------"); 67 document. write ("<br/>"); 68/* 69 6. the logical AND operation in JavaScript is a simple operation. If the first operation determines the result, the second operation is no longer calculated. 70 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. 71 */72 var bFalse = false; 73 var bResult = (bFalse & bUnknown); 74 document. write ("6. the logical AND operation in JavaScript is a simple operation. If the first operation determines the result, the second operation is no longer calculated. "); 75 document. write ("<br/>"); 76 document. write ("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. "); 77 document. write ("<br/>"); 78 document. write ("bFalse = false, bUnknown is an undefined variable. The result of bFalse & bUnknown is:" + (bResult )); // output "false" 79 80 var bTrue = true; 81 var bResult = (bTrue & bUnknown); // error 82 alert (bResult ); // This row will not execute 83 </script>

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.