Logical Operators

  • Revision slug: JavaScript/Reference/Operators/Logical_Operators
  • Revision title: Logical Operators
  • Revision id: 48147
  • Created:
  • Creator: ywairong
  • Is current revision? No
  • Comment no changes

Revision Content

 

Summary

Logical operators are typically used with Boolean (logical) values; when they are, they return a Boolean value. However, the && and || operators actually return the value of one of the specified operands, so if these operators are used with non-Boolean values, they may return a non-Boolean value.

Operators
Implemented in: JavaScript 1.0
ECMA Version: ECMA-262

The logical operators are described in the following table:

Operator Usage Description
Logical AND (&&) expr1 && expr2 Returns expr1 if it can be converted to false; otherwise, returns expr2. Thus, when used with Boolean values, && returns true if both operands are true; otherwise, returns false.
Logical OR (||) expr1 || expr2 Returns expr1 if it can be converted to true; otherwise, returns expr2. Thus, when used with Boolean values, || returns true if either operand is true; if both are false, returns false.
Logical NOT (!) !expr Returns false if its single operand can be converted to true; otherwise, returns true.

Examples of expressions that can be converted to false are those that evaluate to null, 0, the empty string (""), or undefined.

Even though the && and || operators can be used with operands that are not Boolean values, they can still be considered Boolean operators since their return values can always be converted to Boolean values.

Short-Circuit Evaluation

As logical expressions are evaluated left to right, they are tested for possible "short-circuit" evaluation using the following rules:

  • false && anything is short-circuit evaluated to false.
  • true || anything is short-circuit evaluated to true.

The rules of logic guarantee that these evaluations are always correct. Note that the anything part of the above expressions is not evaluated, so any side effects of doing so do not take effect.

For example, the following two functions are equivalent.

function shortCircuitEvaluation() {
  doSomething() || doSomethingElse()
}

function equivalentEvaluation() {
  var flag = doSomething();
  if (!flag) {
    doSomethingElse();
  }
}

Backward Compatibility

JavaScript 1.0 and 1.1

The && and || operators behave as follows:

Operator Usage Behavior
&& expr1 && expr2 If the first operand (expr1) can be converted to false, the && operator returns false rather than the value of expr1.
|| expr1 || expr2 If the first operand (expr1) can be converted to true, the || operator returns true rather than the value of expr1.

Examples

Logical AND (&&)

The following code shows examples of the && (logical AND) operator.

a1=true && true       // t && t returns true
a2=true && false      // t && f returns false
a3=false && true      // f && t returns false
a4=false && (3 == 4)  // f && f returns false
a5="Cat" && "Dog"     // t && t returns Dog
a6=false && "Cat"     // f && t returns false
a7="Cat" && false     // t && f returns false

Logical OR (||)

The following code shows examples of the || (logical OR) operator.

o1=true || true       // t || t returns true
o2=false || true      // f || t returns true
o3=true || false      // t || f returns true
o4=false || (3 == 4)  // f || f returns false
o5="Cat" || "Dog"     // t || t returns Cat
o6=false || "Cat"     // f || t returns Cat
o7="Cat" || false     // t || f returns Cat

Logical NOT (!)

The following code shows examples of the ! (logical NOT) operator.

n1=!true              // !t returns false
n2=!false             // !f returns true
n3=!"Cat"             // !t returns false

 

{{ languages( { "fr": "fr/R\u00e9f\u00e9rence_de_JavaScript_1.5_Core/Op\u00e9rateurs/Op\u00e9rateurs_logiques", "ja": "ja/JavaScript/Reference/Operators/Logical_Operators", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Operatory/Operatory_logiczne" } ) }}

Revision Source

<p> </p>
<h3 id="Summary" name="Summary">Summary</h3>
<p>Logical operators are typically used with Boolean (logical) values; when they are, they return a Boolean value. However, the <code>&amp;&amp;</code> and <code>||</code> operators actually return the value of one of the specified operands, so if these operators are used with non-Boolean values, they may return a non-Boolean value.</p>
<table class="fullwidth-table"> <tbody> <tr> <td class="header" colspan="2">Operators</td> </tr> <tr> <td>Implemented in:</td> <td>JavaScript 1.0</td> </tr> <tr> <td>ECMA Version:</td> <td>ECMA-262</td> </tr> </tbody>
</table>
<p>The logical operators are described in the following table:</p>
<table class="fullwidth-table"> <tbody> <tr> <th>Operator</th> <th>Usage</th> <th>Description</th> </tr> <tr> <td>Logical AND (<code>&amp;&amp;</code>)</td> <td><code><em>expr1</em> &amp;&amp; <em>expr2</em></code></td> <td>Returns <code>expr1</code> if it can be converted to false; otherwise, returns <code>expr2</code>. Thus, when used with Boolean values, <code>&amp;&amp;</code> returns true if both operands are true; otherwise, returns false.</td> </tr> <tr> <td>Logical OR (<code>||</code>)</td> <td><code><em>expr1</em> || <em>expr2</em></code></td> <td>Returns <code>expr1</code> if it can be converted to true; otherwise, returns <code>expr2</code>. Thus, when used with Boolean values, <code>||</code> returns true if either operand is true; if both are false, returns false.</td> </tr> <tr> <td>Logical NOT (<code>!</code>)</td> <td><code>!<em>expr</em></code></td> <td>Returns false if its single operand can be converted to true; otherwise, returns true.</td> </tr> </tbody>
</table>
<p>Examples of expressions that can be converted to false are those that evaluate to null, 0, the empty string (""), or undefined.</p>
<p>Even though the <code>&amp;&amp;</code> and <code>||</code> operators can be used with operands that are not Boolean values, they can still be considered Boolean operators since their return values can always be converted to Boolean values.</p>
<h4 id="Short-Circuit_Evaluation" name="Short-Circuit_Evaluation">Short-Circuit Evaluation</h4>
<p>As logical expressions are evaluated left to right, they are tested for possible "short-circuit" evaluation using the following rules:</p>
<ul> <li><code>false &amp;&amp; <em>anything</em></code> is short-circuit evaluated to false.</li> <li><code>true || <em>anything</em></code> is short-circuit evaluated to true.</li>
</ul>
<p>The rules of logic guarantee that these evaluations are always correct. Note that the anything part of the above expressions is not evaluated, so any side effects of doing so do not take effect.</p>
<p>For example, the following two functions are equivalent.</p>
<pre>function shortCircuitEvaluation() {
  doSomething() || doSomethingElse()
}

function equivalentEvaluation() {
  var flag = doSomething();
  if (!flag) {
    doSomethingElse();
  }
}
</pre>
<h3 id="Backward_Compatibility" name="Backward_Compatibility">Backward Compatibility</h3>
<h4 id="JavaScript_1.0_and_1.1" name="JavaScript_1.0_and_1.1">JavaScript 1.0 and 1.1</h4>
<p>The <code>&amp;&amp;</code> and <code>||</code> operators behave as follows:</p>
<table class="fullwidth-table"> <tbody> <tr> <th>Operator</th> <th>Usage</th> <th>Behavior</th> </tr> <tr> <td><code>&amp;&amp;</code></td> <td><code><em>expr1</em> &amp;&amp; <em>expr2</em></code></td> <td>If the first operand (<code>expr1</code>) can be converted to false, the <code>&amp;&amp;</code> operator returns false rather than the value of <code>expr1</code>.</td> </tr> <tr> <td><code>||</code></td> <td><code><em>expr1</em> || <em>expr2</em></code></td> <td>If the first operand (<code>expr1</code>) can be converted to true, the <code>||</code> operator returns true rather than the value of <code>expr1</code>.</td> </tr> </tbody>
</table>
<h3 id="Examples" name="Examples">Examples</h3>
<h4 id="Logical_AND_.28&amp;&amp;.29" name="Logical_AND_.28&amp;&amp;.29">Logical AND (<code>&amp;&amp;</code>)</h4>
<p>The following code shows examples of the <code>&amp;&amp;</code> (logical AND) operator.</p>
<pre>a1=true &amp;&amp; true       // t &amp;&amp; t returns true
a2=true &amp;&amp; false      // t &amp;&amp; f returns false
a3=false &amp;&amp; true      // f &amp;&amp; t returns false
a4=false &amp;&amp; (3 == 4)  // f &amp;&amp; f returns false
a5="Cat" &amp;&amp; "Dog"     // t &amp;&amp; t returns Dog
a6=false &amp;&amp; "Cat"     // f &amp;&amp; t returns false
a7="Cat" &amp;&amp; false     // t &amp;&amp; f returns false
</pre>
<h4 id="Logical_OR_.28||.29" name="Logical_OR_.28||.29">Logical OR (<code>||</code>)</h4>
<p>The following code shows examples of the <code>||</code> (logical OR) operator.</p>
<pre>o1=true || true       // t || t returns true
o2=false || true      // f || t returns true
o3=true || false      // t || f returns true
o4=false || (3 == 4)  // f || f returns false
o5="Cat" || "Dog"     // t || t returns Cat
o6=false || "Cat"     // f || t returns Cat
o7="Cat" || false     // t || f returns Cat
</pre>
<h4 id="Logical_NOT_.28.21.29" name="Logical_NOT_.28.21.29">Logical NOT (<code>!</code>)</h4>
<p>The following code shows examples of the <code>!</code> (logical NOT) operator.</p>
<pre>n1=!true              // !t returns false
n2=!false             // !f returns true
n3=!"Cat"             // !t returns false
</pre>
<p> </p>
<p>{{ languages( { "fr": "fr/R\u00e9f\u00e9rence_de_JavaScript_1.5_Core/Op\u00e9rateurs/Op\u00e9rateurs_logiques", "ja": "ja/JavaScript/Reference/Operators/Logical_Operators", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Operatory/Operatory_logiczne" } ) }}</p>
Revert to this revision