Logical AND (&&) (Operator/logical)

Logical AND of two operands.

Availability:

ECMAScript edition - 2
JavaScript - 1.0
JScript - 1.0
Internet Explorer - 3.02
Netscape - 2.0
Netscape Enterprise Server - 2.0
Opera - 3.0
Property/method value type:Boolean primitive
JavaScript syntax:-anOperand1 && anOperand2
Argument list:anOperand1A Boolean value
anOperand2Another Boolean value

Traditionally in programming environments, the logical AND operator yields true only when both operands are true. However, the specifics of this are slightly different in JavaScript, and although the results may appear to be functionally the same, there is a subtle but important difference.

First, lets deal with the normal and expected behavior of a Logical AND operator. The truth table shows the result of this operator for two Boolean primitive values:

ABAND
falsefalsefalse
falsetruefalse
truefalsefalse
truetruetrue

Now, the implementation is expected to conform to the ECMA standard. This sets out the following method of evaluation for a Logical AND operator:

To all intents and purposes the external perceived behavior is the same because another ToBoolean() conversion is likely to take place in the context that the expression is used - an if() statement or an outer logical expression for example.

The associativity is from left to right.

Refer to the operator precedence topic for details of execution order.

The result is the Boolean value true if both operands are true, otherwise Boolean false is returned.

Example code:

   <HTML>
   <HEAD></HEAD>
   <BODY>
   <TABLE BORDER=1 CELLPADDING=2>
   <TR>
   <TH>A</TH>
   <TH>B</TH>
   <TH>AND</TH>
   </TR>
   <SCRIPT>
   for(a=0; a<2; a++)
   {
      for(b=0; b<2; b++)
      {
         document.write("<TR ALIGN=CENTER><TD>");
         document.write(Boolean(a));
         document.write("</TD><TD>");
         document.write(Boolean(b));
         document.write("</TD><TD>");
         document.write(Boolean(a && b));
         document.write("</TD></TR>");
      }
   }
   </SCRIPT>
   </TABLE>
   </BODY>
   </HTML>

See also:Associativity, Binary logical operator, Bitwise AND (&), Bitwise AND then assign (&=), Logical expression, Logical operator, Operator Precedence

insert figure 0031

Cross-references:

ECMA 262 edition 2 - section - 11.11

ECMA 262 edition 3 - section - 11.11