Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 997221 of Logical Operators

  • Revision slug: Web/JavaScript/Reference/Operators/Logical_Operators
  • Revision title: Logical Operators
  • Revision id: 997221
  • Created:
  • Creator: fscholz
  • Is current revision? No
  • Comment Add Glossary link

Revision Content

{{jsSidebar("Operators")}}

Logical operators are typically used with {{jsxref("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.

Description

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 can be converted to 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 can be converted to true; if both can be converted to false, returns false.
Logical NOT (!) !expr Returns false if its single operand can be converted to true; otherwise, returns true.

If a value can be converted to true, the value is so-called {{Glossary("truthy")}}. If a value can be converted to false, the value is so-called {{Glossary("falsy")}}.

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. Also note that the anything part of the above expression is any single logical expression (as indicated by the parentheses).

For example, the following two functions are equivalent.

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

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

However, the following expressions are not equivalent due to operator precedence, and stresses the importance of requiring the right hand operator to be a single expression (grouped if needed by parentheses).

false && true  || true      // returns true
false && (true || true)     // returns false

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

Conversion rules

Converting AND to OR

the following operation involving Booleans:

bCondition1 && bCondition2

is always equal to:

!(!bCondition1 || !bCondition2)

Converting OR to AND

the following operation involving Booleans:

bCondition1 || bCondition2

is always equal to:

!(!bCondition1 && !bCondition2)

Converting between NOTs

the following operation involving Booleans:

!!bCondition

is always equal to:

bCondition

Removing nested parentheses

As logical expressions are evaluated left to right, it is always possible to remove parentheses from a complex expression following some rules.

Removing nested AND

The following composite operation involving Booleans:

bCondition1 || (bCondition2 && bCondition3)

is always equal to:

bCondition1 || bCondition2 && bCondition3

Removing nested OR

The following composite operation involving Booleans:

bCondition1 && (bCondition2 || bCondition3)

is always equal to:

!(!bCondition1 || !bCondition2 && !bCondition3)

Specifications

Specification Status Comment
{{SpecName('ES1')}} {{Spec2('ES1')}} Initial definition.
{{SpecName('ES5.1', '#sec-11.11')}} {{Spec2('ES5.1')}} Defined in several sections of the specification: Logical NOT Operator, Binary Logical Operators
{{SpecName('ES6', '#sec-binary-logical-operators')}} {{Spec2('ES6')}} Defined in several sections of the specification: Logical NOT Operator, Binary Logical Operators
{{SpecName('ESDraft', '#sec-binary-logical-operators')}} {{Spec2('ESDraft')}} Defined in several sections of the specification: Logical NOT Operator, Binary Logical Operators

Browser compatibility

{{CompatibilityTable}}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Logical AND (&&) {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}}
Logical OR (||) {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}}
Logical NOT (!) {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}}
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Logical AND (&&) {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}}
Logical OR (||) {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}}
Logical NOT (!) {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}}

See also

Revision Source

<div>{{jsSidebar("Operators")}}</div>

<p>Logical operators are typically used with {{jsxref("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>

<h2 id="Description">Description</h2>

<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 can be converted to 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 can be converted to true; if both can be converted to 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>If a value can be converted to <code>true</code>, the value is so-called {{Glossary("truthy")}}. If a value can be converted to <code>false</code>, the value is so-called {{Glossary("falsy")}}.</p>

<p>Examples of expressions that can be converted to <code>false</code> are those that evaluate to null, 0, the empty string (""), or <code>undefined</code>.</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>

<h3 id="Short-Circuit_Evaluation">Short-Circuit Evaluation</h3>

<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. Also note that the anything part of the above expression is any single logical expression (as indicated by the parentheses).</p>

<p>For example, the following two functions are equivalent.</p>

<pre class="brush: js">
function shortCircuitEvaluation() {
&nbsp;&nbsp;doSomething() || doSomethingElse()
}

function equivalentEvaluation() {
&nbsp; var flag = doSomething();
&nbsp; if (!flag) {
&nbsp; &nbsp; doSomethingElse();
&nbsp; }
}
</pre>

<p>However, the following expressions are not equivalent due to <a href="/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence">operator precedence</a>, and stresses the importance of requiring the right hand operator to be a single expression (grouped if needed by parentheses).</p>

<pre class="brush: js">
false &amp;&amp; true  || true      // returns true
false &amp;&amp; (true || true)     // returns false</pre>

<h3 id="Logical_AND_()"><a name="Logical_AND">Logical AND (<code>&amp;&amp;</code>)</a></h3>

<p>The following code shows examples of the <code>&amp;&amp;</code> (logical AND) operator.</p>

<pre class="brush: js">
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>

<h3 id="Logical_OR_()"><a name="Logical_OR">Logical OR (<code>||</code>)</a></h3>

<p>The following code shows examples of the <code>||</code> (logical OR) operator.</p>

<pre class="brush: js">
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>

<h3 id="Logical_NOT_(!)"><a name="Logical_NOT">Logical NOT (<code>!</code>)</a></h3>

<p>The following code shows examples of the <code>!</code> (logical NOT) operator.</p>

<pre class="brush: js">
n1 = !true              //&nbsp;!t returns false
n2 = !false             //&nbsp;!f returns true
n3 = !"Cat"             //&nbsp;!t returns false
</pre>

<h3 id="Conversion_rules">Conversion rules</h3>

<h4 id="Converting_AND_to_OR">Converting AND to OR</h4>

<p>the following operation involving Booleans:</p>

<pre class="brush: js">
bCondition1 &amp;&amp; bCondition2</pre>

<p>is always equal to:</p>

<pre class="brush: js">
!(!bCondition1 || !bCondition2)</pre>

<h4 id="Converting_OR_to_AND">Converting OR to AND</h4>

<p>the following operation involving Booleans:</p>

<pre class="brush: js">
bCondition1 || bCondition2</pre>

<p>is always equal to:</p>

<pre class="brush: js">
!(!bCondition1 &amp;&amp; !bCondition2)</pre>

<h4 id="Converting_between_NOTs">Converting between NOTs</h4>

<p>the following operation involving Booleans:</p>

<pre class="brush: js">
!!bCondition</pre>

<p>is always equal to:</p>

<pre class="brush: js">
bCondition</pre>

<h3 id="Removing_nested_parentheses">Removing nested parentheses</h3>

<p>As logical expressions are evaluated left to right, it is always possible to remove parentheses from a complex expression following some rules.</p>

<h4 id="Removing_nested_AND">Removing nested AND</h4>

<p>The following composite operation involving Booleans:</p>

<pre class="brush: js">
bCondition1 || (bCondition2 &amp;&amp; bCondition3)</pre>

<p>is always equal to:</p>

<pre class="brush: js">
bCondition1 || bCondition2 &amp;&amp; bCondition3</pre>

<h4 id="Removing_nested_OR">Removing nested OR</h4>

<p>The following composite operation involving Booleans:</p>

<pre class="brush: js">
bCondition1 &amp;&amp; (bCondition2 || bCondition3)</pre>

<p>is always equal to:</p>

<pre class="brush: js">
!(!bCondition1 || !bCondition2 &amp;&amp; !bCondition3)</pre>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('ES1')}}</td>
   <td>{{Spec2('ES1')}}</td>
   <td>Initial definition.</td>
  </tr>
  <tr>
   <td>{{SpecName('ES5.1', '#sec-11.11')}}</td>
   <td>{{Spec2('ES5.1')}}</td>
   <td>Defined in several sections of the specification: <a href="https://www.ecma-international.org/ecma-262/5.1/#sec-11.4.9">Logical NOT Operator</a>, <a href="https://www.ecma-international.org/ecma-262/5.1/#sec-11.11">Binary Logical Operators</a></td>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-binary-logical-operators')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td>Defined in several sections of the specification: <a href="https://www.ecma-international.org/ecma-262/6.0/#sec-logical-not-operator">Logical NOT Operator</a>, <a href="https://www.ecma-international.org/ecma-262/6.0/#sec-binary-logical-operators">Binary Logical Operators</a></td>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-binary-logical-operators')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td>Defined in several sections of the specification: <a href="https://tc39.github.io/ecma262/#sec-logical-not-operator">Logical NOT Operator</a>, <a href="https://tc39.github.io/ecma262/#sec-binary-logical-operators">Binary Logical Operators</a></td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<p>{{CompatibilityTable}}</p>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Logical AND (<code>&amp;&amp;</code>)</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
  <tr>
   <td>Logical OR (<code>||</code>)</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
  <tr>
   <td>Logical NOT (<code>!</code>)</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Chrome for Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Logical AND (<code>&amp;&amp;</code>)</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
  <tr>
   <td>Logical OR (<code>||</code>)</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
  <tr>
   <td>Logical NOT (<code>!</code>)</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<h2 id="See_also">See also</h2>

<ul>
 <li><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators">Bitwise operators</a></li>
 <li><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean">Boolean</a></li>
</ul>
Revert to this revision