<?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: Arijit Ray</title>
    <description>The latest articles on DEV Community by Arijit Ray (@itsarijitray).</description>
    <link>https://dev.to/itsarijitray</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%2F708411%2F47a01de7-8ae9-4d99-a0e6-b5b7e895878c.png</url>
      <title>DEV Community: Arijit Ray</title>
      <link>https://dev.to/itsarijitray</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/itsarijitray"/>
    <language>en</language>
    <item>
      <title>Decoding Javascript: The Nullish coalescing operator '??' is awesome!</title>
      <dc:creator>Arijit Ray</dc:creator>
      <pubDate>Sun, 25 Jun 2023 06:41:54 +0000</pubDate>
      <link>https://dev.to/itsarijitray/decoding-javascript-the-nullish-coalescing-operator-is-awesome-41e2</link>
      <guid>https://dev.to/itsarijitray/decoding-javascript-the-nullish-coalescing-operator-is-awesome-41e2</guid>
      <description>&lt;p&gt;A few days back, I was working on a project of mine where I wanted to set the value to a variable &lt;code&gt;enabled&lt;/code&gt; based on a particular property of an object &lt;code&gt;config&lt;/code&gt;. The interface for the object looks something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;interface IAuthConfig {
  enabledCookieAuth?: boolean;
  // other properties
}

function setDefault(config: IAuthConfig) {
  const enabled = config?.enabledCookieAuth
  //rest of the code
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For the variable &lt;code&gt;enabled&lt;/code&gt;, I wanted to set the value of &lt;code&gt;enabledCookieAuth&lt;/code&gt; of the &lt;code&gt;config&lt;/code&gt;. Since &lt;code&gt;enabledCookieAuth&lt;/code&gt; was an optional property, I wanted to set the default value of &lt;code&gt;enabled&lt;/code&gt; &lt;code&gt;true&lt;/code&gt; in case &lt;code&gt;enabledCookieAuth&lt;/code&gt; was &lt;code&gt;null&lt;/code&gt; or &lt;code&gt;undefined&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;ENTER Nullish coalescing &lt;code&gt;??&lt;/code&gt; operator.&lt;/p&gt;

&lt;p&gt;This particular operator is perfect for this requirement. The way it works is, it returns the first value that is &lt;strong&gt;defined&lt;/strong&gt;. The code looks something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const enabled = config?.enabledCookieAuth ?? true 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above code works exactly the same as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const enabled = (config?.enabledCookieAuth !== null &amp;amp;&amp;amp; config?.enabledCookieAuth !== undefined)? config?.enabledCookieAuth : true 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the question that you might ask is, Why not use &lt;code&gt;||&lt;/code&gt; OR operator?&lt;/p&gt;

&lt;p&gt;Well, as I said earlier, &lt;code&gt;??&lt;/code&gt; returns the first value that is &lt;strong&gt;defined&lt;/strong&gt; whereas &lt;code&gt;||&lt;/code&gt; returns the first value that is not &lt;strong&gt;falsy&lt;/strong&gt;. &lt;code&gt;||&lt;/code&gt; OR operator treats empty string (&lt;code&gt;''&lt;/code&gt;), &lt;code&gt;0&lt;/code&gt; or &lt;code&gt;false&lt;/code&gt; as a falsy value. Meaning, even if &lt;code&gt;config?.enabledCookieAuth&lt;/code&gt; is defined and set to &lt;code&gt;false&lt;/code&gt;, &lt;code&gt;config?.enabledCookieAuth || true&lt;/code&gt; will return &lt;code&gt;true&lt;/code&gt; which doesn't serve our particular case.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const value1 = false
console.log(value1 ?? true)
// returns false as value1 is defined. 
console.log(value || true)
// returns true in all cases
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Although this operator has been there for quite sometime, it has been mostly unknown to the majority of the community.&lt;/p&gt;

&lt;p&gt;Do note that it might not work with old browsers. &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing#browser_compatibility"&gt;Click here to check out its compatibility.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For further reference: Check out &lt;a href="https://javascript.info/nullish-coalescing-operator"&gt;Nullish coalescing operator '??'&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>tutorial</category>
      <category>typescript</category>
    </item>
  </channel>
</rss>
