Detailed explanation of JavaScript logical AND operator _javascript tips

Source: Internet
Author: User
Tags arithmetic

In JavaScript, the logical AND operator is represented by a double ampersand (&&)

var btrue = true;
var bfalse = false;
var bresult = btrue && bfalse;

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

What needs to be explained is that the operands of a logical AND operation can be of any type, not just a Boolean value, and that a logical and operation does not necessarily return a Boolean value if one of the operands is not of the original Boolean value

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

    • If one of the operands is an object and the other is a Boolean value, the object is returned.
    • If the two operands are objects, the second object is returned.
    • Returns null if a shipment count is null.
    • If a shipping count is Nan, return nan.
    • If a shipment count is undefined, an error occurs.
    • Returns a Boolean value if the two operands are all Boolean types

Similar to the logical AND operation in Java, the logical AND operation in JavaScript is also a simple operation, that is, if the first arithmetic determines the result, the second operand is no longer counted. For a logical AND operation, if the first operand is false, the result cannot be equal to true regardless of the value of the second operand.
Consider the following example:

var btrue = true;
var bresult = (btrue && bunknown); Error
alert (bresult) occurred;//This line will not execute

Code Run Results:

This code throws an error when doing a logical and operation, because the variable bunknown is undefined. The value of the variable btrue is true because the logical AND operation will continue to compute the variable bunknown. Doing so raises an error because the value of the bunknown is undefined and cannot be used for logical and operations.
If you modify this example to set the first number to false, no error occurs:

var btrue = false;
var bresult = (btrue && bunknown); Error alert will not occur
("Btrue && bunknown results are:" + (Bresult));/output "false"

In this code, the script will output the value returned by the logical and operation, that is, the string "false". Even if the value of the variable bunknown is undefined, it will not be evaluated because the value of the first operand is false.

Run Result:

Verifying the operational behavior of the JavaScript logical AND operator

Test code:

<script type= "Text/javascript" > document.write ("Verifying the operational behavior of the JavaScript logic and Operator:");
  document.write ("<br/>");
  document.write ("-----------------------------------------------------------------------------");

  document.write ("<br/>");
  var btrue = true;
  var bfalse = false;
  var bresult = btrue && bfalse;
  /*1. If the two operands are all Boolean types, returns a Boolean value of/document.write ("1. Boolean value if two operands are Boolean");
  document.write ("<br/>");
  document.write ("-----------------------------------------------------------------------------");
  document.write ("<br/>"); document.write ("btrue = True,bfalse = false,btrue && bfalse result is:" + (Bresult));//The result is false document.write ("<

  Br/> ");
  document.write ("-----------------------------------------------------------------------------");

  document.write ("<br/>");
  var obj = new Object ();
  /*2. If one operand is an object, and the other is a Boolean value, return the object/document.write ("2. If one of the operands is an object and the other is a Boolean value, return the object"); document.write ("&LT;br/> ");
  document.write ("-----------------------------------------------------------------------------");
  document.write ("<br/>");
  document.write ("obj is an object, the true&&obj result is:" + (true&&obj));
  document.write ("<br/>");
  The result of document.write ("Obj==true&&obj) is:" + (obj== (true&&obj));
  document.write ("<br/>");
  document.write ("False&&obj's result is:" + (false&&obj));

  document.write ("<br/>");
  document.write ("-----------------------------------------------------------------------------");
  document.write ("<br/>"); /*3. If the two operands are objects, the second object is returned.
  * * var obj1 = new Object ();
  var obj2 = new Object (); document.write ("3. If the two operands are objects, the second object is returned.)
  ");
  document.write ("<br/>");
  document.write ("-----------------------------------------------------------------------------");
  document.write ("<br/>");
  document.write ("Obj1 is an object, Obj2 is an object, the result of obj1== (OBJ1&AMP;&AMP;OBJ2) is:" + (obj1== (obj1&&obj2)); TodoCument.write ("<br/>");
  document.write ("Obj1 is an object, Obj2 is an object, the result of obj2== (OBJ1&AMP;&AMP;OBJ2) is:" + (obj2== (OBJ1&AMP;&AMP;OBJ2));//The result is true

  document.write ("<br/>");
  document.write ("-----------------------------------------------------------------------------");
  document.write ("<br/>"); /*4. If a shipment count is NULL, return NULL.
  * * var a=null;
  var b=true; document.write ("4. If a shipment count is NULL, return null.")
  ");
  document.write ("<br/>");
  document.write ("A=null,b=true,a&&b's result is:" + (a&&b));
  document.write ("<br/>");
  document.write ("-----------------------------------------------------------------------------");
  document.write ("<br/>");
   /*5. If a shipping count is Nan, return nan*/var c = nan;
  var d = "Str";
  document.write ("5. If a shipping count is Nan, return nan");
  document.write ("<br/>");
  document.write ("C=nan,d=str,c&&d's result is:" + (c&&d));
  document.write ("<br/>"); document.write ("-----------------------------------------------------------------------------");
  document.write ("<br/>");
  The logical AND operation in/* 6.JavaScript is a simple operation, that is, if the first arithmetic determines the result, the second one is no longer counted.
  For a logical AND operation, if the first operand is false, the result cannot be equal to true regardless of the value of the second operand.
  * * var bfalse = false;
  var bresult = (bfalse && bunknown);
  The logical AND operation in the document.write ("6.JavaScript) is a simple operation, that is, if the first arithmetic determines the result, the second operation is no longer counted");
  document.write ("<br/>"); document.write ("For a logical AND operation, if the first count is false, the result cannot be equal to true regardless of the value of the second operand.")
  ");
  document.write ("<br/>"); document.write ("Bfalse=false,bunknown is an undefined variable, the result of Bfalse && Bunknown is:" + (Bresult));
  Output "false" var btrue = true;  var bresult = (btrue && bunknown);      Error alert (bresult) occurred;

 This line does not perform </script>

This is all about the JavaScript logic and operators, I hope to help you better learn the logical operator JS.

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.