<?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>Forem: Piyush Das</title>
    <description>The latest articles on Forem by Piyush Das (@coldpigli).</description>
    <link>https://forem.com/coldpigli</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%2F698705%2Fd8475afb-3228-4970-bc26-cb69694748f2.jpeg</url>
      <title>Forem: Piyush Das</title>
      <link>https://forem.com/coldpigli</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/coldpigli"/>
    <language>en</language>
    <item>
      <title>Nullish Coalescing operator in JS "??" ?</title>
      <dc:creator>Piyush Das</dc:creator>
      <pubDate>Sun, 15 May 2022 15:54:44 +0000</pubDate>
      <link>https://forem.com/coldpigli/nullish-coalescing-operator-in-js--4l2c</link>
      <guid>https://forem.com/coldpigli/nullish-coalescing-operator-in-js--4l2c</guid>
      <description>&lt;p&gt;I was intrigued about the Nullish Coalescing Operator ever since I had heard about it, primarily because it sounded very fancy X'D. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--OMklLXH_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2a3ps8elwyyntdhsnai3.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--OMklLXH_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2a3ps8elwyyntdhsnai3.jpg" alt="Gentleman Bear" width="225" height="225"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I decided to learn more about it. Hence this blog. I'll try to keep things as simple and beginner friendly as possible.&lt;/p&gt;

&lt;p&gt;Allright, let's get right into it then!&lt;/p&gt;

&lt;h2&gt;
  
  
  What the heck is "??" ?
&lt;/h2&gt;

&lt;p&gt;The nullish coalescing operator is syntactically defined as '??'&lt;br&gt;
Basically, it keeps an eye open for 'null' and 'undefined' values.&lt;br&gt;
Let's see how with the help of an example.&lt;/p&gt;

&lt;p&gt;Suppose we have the following expression:&lt;br&gt;
&lt;code&gt;const fruitCount = noOfApples ?? noOfOranges&lt;/code&gt;&lt;br&gt;
Then the value of fruit will be dependent upon the value of apples. If &lt;code&gt;noOfApples&lt;/code&gt; is &lt;code&gt;null or undefined&lt;/code&gt;, then the value of &lt;code&gt;fruitCount&lt;/code&gt; will be equal to &lt;code&gt;noOfOranges&lt;/code&gt;. Contrary to this, if the value of &lt;code&gt;noOfApples&lt;/code&gt; is anything other than &lt;code&gt;null&lt;/code&gt; or &lt;code&gt;undefined&lt;/code&gt;, then &lt;code&gt;fruitCount&lt;/code&gt; will be equal to &lt;code&gt;noOfApples&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;To summarise things:&lt;br&gt;
In an expression such as &lt;code&gt;a??b&lt;/code&gt;, the result is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;a&lt;/code&gt;, if a has any value other than &lt;code&gt;null&lt;/code&gt; or &lt;code&gt;undefined&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;b&lt;/code&gt;, if a is &lt;code&gt;null&lt;/code&gt; or &lt;code&gt;undefined&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It's basically a syntactic sugar over&lt;br&gt;
&lt;code&gt;result = (a !== null &amp;amp;&amp;amp; a !== undefined) ? a : b;&lt;/code&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Common use cases of the Nullish Coalescing Operator
&lt;/h2&gt;
&lt;h3&gt;
  
  
  1) Assigning a default value to a variable
&lt;/h3&gt;

&lt;p&gt;The nullish coalescing operator makes it easy for you to provide a default value to a variable that has not been assigned one.&lt;/p&gt;

&lt;p&gt;For example,&lt;br&gt;
&lt;code&gt;let count;&lt;/code&gt;&lt;br&gt;
&lt;code&gt;const sum = count ?? 0;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;In the above example, since count it not initialized with a value hence the value of sum is provided by the second operand which is &lt;code&gt;0&lt;/code&gt; in this case.&lt;/p&gt;
&lt;h3&gt;
  
  
  2) Selecting from a list of undefined/null values
&lt;/h3&gt;

&lt;p&gt;Let's take into consideration a scenario where we have to display the name of a user. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;let firstName = null;&lt;/code&gt;&lt;br&gt;
&lt;code&gt;let lastName = null;&lt;/code&gt;&lt;br&gt;
&lt;code&gt;let nickName = "Coldpigli";&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;// shows the first defined value:&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;console.log(firstName ?? lastName ?? nickName ?? "User");&lt;/code&gt; &lt;br&gt;
&lt;code&gt;// Coldpigli&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;In the above example the &lt;code&gt;Coldpigli&lt;/code&gt; is logged since &lt;code&gt;firstName&lt;/code&gt; and &lt;code&gt;lastName&lt;/code&gt; are &lt;code&gt;null&lt;/code&gt; and &lt;code&gt;nickName&lt;/code&gt; provides a "defined" value (in the "??" sense of things). &lt;/p&gt;
&lt;h2&gt;
  
  
  Okay, but how is that different from '||' ?
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;||&lt;/code&gt; can be used as &lt;code&gt;??&lt;/code&gt; but there are some cases when you need to be wary of using &lt;code&gt;||&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The important difference between them is that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;|| returns the first truthy value.&lt;/li&gt;
&lt;li&gt;?? returns the first defined value.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That means, &lt;code&gt;||&lt;/code&gt; cannot distinguish between &lt;code&gt;false&lt;/code&gt;, &lt;code&gt;0&lt;/code&gt;, an empty string &lt;code&gt;""&lt;/code&gt; and &lt;code&gt;null/undefined&lt;/code&gt;.&lt;br&gt;
This can be a serious problems in cases when we want to treat &lt;code&gt;0&lt;/code&gt; or &lt;code&gt;false&lt;/code&gt; as values and not absence of it.&lt;/p&gt;

&lt;p&gt;Let's understand this with the help of an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let count = 0;
console.log(count || 100); //100
console.log(count ?? 100); //0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Count 0 can be perfectly valid value for a variable that counts something.&lt;br&gt;
The &lt;code&gt;count || 100&lt;/code&gt; checks height for being a falsy value, and it’s &lt;code&gt;0&lt;/code&gt;, which is falsy&lt;br&gt;
so the result of || is the second argument, 100.&lt;br&gt;
The &lt;code&gt;count ?? 100&lt;/code&gt; checks count for being null/undefined, and it’s not&lt;br&gt;
so the result is count “as is”, that is 0.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using ?? with &amp;amp;&amp;amp; or ||
&lt;/h2&gt;

&lt;p&gt;Javascript forbids using &lt;code&gt;??&lt;/code&gt; in conjunction with &lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt; or &lt;code&gt;||&lt;/code&gt;.&lt;br&gt;
Therefor an expression such as &lt;code&gt;a = b &amp;amp;&amp;amp; c || d&lt;/code&gt; will result in syntax error.&lt;/p&gt;

&lt;p&gt;However, there is way around it. &lt;br&gt;
You can prioritize the usage explicitly by using &lt;code&gt;()&lt;/code&gt; such as,&lt;br&gt;
&lt;code&gt;a = (b &amp;amp;&amp;amp; c) || d&lt;/code&gt;. &lt;br&gt;
The above is a perfectly valid Javascript expression.&lt;/p&gt;

&lt;p&gt;Alright, that brings me to the end of this blog. I hope the nullish coalescing operator doesn't seem as intimidating as it sounds anymore.&lt;/p&gt;

&lt;p&gt;Until next time folks ! &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>es6</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
