<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Chinedu Ikechi</title>
    <description>The latest articles on DEV Community by Chinedu Ikechi (@chinedu__ikechi).</description>
    <link>https://dev.to/chinedu__ikechi</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F269592%2Fdaf8c95a-a9c4-4d58-b86c-9e194910c74c.jpeg</url>
      <title>DEV Community: Chinedu Ikechi</title>
      <link>https://dev.to/chinedu__ikechi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/chinedu__ikechi"/>
    <language>en</language>
    <item>
      <title>|| and &amp;&amp; are not "logical operators" in JavaScript</title>
      <dc:creator>Chinedu Ikechi</dc:creator>
      <pubDate>Mon, 04 Jan 2021 20:35:57 +0000</pubDate>
      <link>https://dev.to/chinedu__ikechi/and-are-not-logical-operators-in-javascript-2im1</link>
      <guid>https://dev.to/chinedu__ikechi/and-are-not-logical-operators-in-javascript-2im1</guid>
      <description>&lt;p&gt;Hear me out.&lt;/p&gt;

&lt;p&gt;If you’ve written software with PHP or any statically typed language, you’d know that the value of any expression including either || or &amp;amp;&amp;amp; will return a boolean.&lt;/p&gt;

&lt;p&gt;Enter JavaScript.&lt;/p&gt;

&lt;p&gt;The name “logical operators” doesn’t completely describe the function of || and &amp;amp;&amp;amp; in JavaScript. A more apt name would be “selector operators.”&lt;/p&gt;

&lt;p&gt;This is because, in JavaScript, || or &amp;amp;&amp;amp; will return one (and only) of the two expression values rather than a boolean value.&lt;/p&gt;

&lt;p&gt;Quoting the &lt;a href="https://www.ecma-international.org/ecma-262/5.1/#sec-11.11"&gt;ES5 spec from section 11.11&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The value produced by a &amp;amp;&amp;amp; or || operator is not necessarily of type Boolean. The value produced will always be the value of one the two operand expressions.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Take a quick look at this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var a = 10;
var b = "foo"
var c = null;

a || b  // 10
a &amp;amp;&amp;amp; b  // "foo"

c || b  // "foo"
c &amp;amp;&amp;amp; b  // null
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Surprised?&lt;/p&gt;

&lt;p&gt;Both || and &amp;amp;&amp;amp; operators perform a boolean test on the first expression value (&lt;em&gt;a&lt;/em&gt; or &lt;em&gt;c&lt;/em&gt;). If the value is not already boolean (as it’s not, here), a &lt;a href="https://www.ecma-international.org/ecma-262/5.1/#sec-9.2"&gt;ToBoolean coercion&lt;/a&gt; occurs, so that the test can be performed.&lt;/p&gt;

&lt;p&gt;For the || operator, if the test is true, the || expression results in the first expression value (&lt;em&gt;a&lt;/em&gt; or &lt;em&gt;c&lt;/em&gt;). If the test is false, the || expression results in the second expression value (&lt;em&gt;b&lt;/em&gt;).&lt;/p&gt;

&lt;p&gt;Oppositely, for the &amp;amp;&amp;amp; operator, if the test is true, the &amp;amp;&amp;amp; expression results in the second expression value. If the test is false, the &amp;amp;&amp;amp; expression results in the first expression value.&lt;/p&gt;

&lt;p&gt;Let’s take a deep look at the first and last expressions from the code above for better understanding:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;a || b;  // 10
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above expression, the || operator will first check if the first expression value (&lt;em&gt;a&lt;/em&gt;) is a boolean, else a ToBoolean coercion occurs. &lt;em&gt;a&lt;/em&gt; (10) is not boolean, hence, &lt;em&gt;a&lt;/em&gt; will be coerced to true because &lt;em&gt;a&lt;/em&gt; (10) is a truthy.&lt;/p&gt;

&lt;p&gt;Since our test is true, the || operator will return the first expression value (10) – not the coerced value (true).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;c &amp;amp;&amp;amp; b;  // null
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above expression, the &amp;amp;&amp;amp; operator will first check if the first expression value is boolean, else it coerces it. &lt;em&gt;c&lt;/em&gt; (null) is not boolean, hence, it will be coerced to false, because null is falsy.&lt;/p&gt;

&lt;p&gt;&amp;amp;&amp;amp; operator returns the first expression value if it's falsy or the second expression value if the first value is truthy. Since &lt;em&gt;c&lt;/em&gt;(null) is falsy, the &amp;amp;&amp;amp; operator will return it.&lt;/p&gt;

&lt;p&gt;As a JavaScript programmer, always remember that:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The value produced by a &amp;amp;&amp;amp; or || operator is not necessarily of type Boolean. The value produced will always be the value of one the two operand expressions.&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>advanced</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
