<?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: Subaash</title>
    <description>The latest articles on DEV Community by Subaash (@subaash_b).</description>
    <link>https://dev.to/subaash_b</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%2F1217966%2Fde621225-66c5-4e5d-a0cf-b3932147823d.jpg</url>
      <title>DEV Community: Subaash</title>
      <link>https://dev.to/subaash_b</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/subaash_b"/>
    <language>en</language>
    <item>
      <title>Referential equality: Where variables check-in and rarely check-out.</title>
      <dc:creator>Subaash</dc:creator>
      <pubDate>Fri, 19 Jan 2024 17:05:14 +0000</pubDate>
      <link>https://dev.to/subaash_b/referential-equality-where-variables-check-in-and-rarely-check-out-14gp</link>
      <guid>https://dev.to/subaash_b/referential-equality-where-variables-check-in-and-rarely-check-out-14gp</guid>
      <description>&lt;p&gt;Hi fellow devs, we are here to dive deep into the heart of JavaScript's strict equality operator ('===') and figure out why some variables behave like identical twins while others behave like awkward strangers in an alumni reunion.&lt;br&gt;
Before knowing about the equality operator, we should understand what are primitive values and non-primitive values in JavaScript. &lt;/p&gt;
&lt;h2&gt;
  
  
  Primitive values
&lt;/h2&gt;

&lt;p&gt;Primitives are basic, fundamental data types that serve as the building blocks to build a complex data type. The following are some primitives in JavaScript. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;String&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Number&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Boolean&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Undefined&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Null&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Non-Primitives
&lt;/h2&gt;

&lt;p&gt;Any data type other than the primitives are Non-Primitives.  Some of them are, &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Array&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Object&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Function&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;RegExp&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Set&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Map&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Promise&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Examples
&lt;/h2&gt;

&lt;p&gt;1) Primitives and the effect of Equality&lt;br&gt;
Whenever we strictly equate('===') two primitives of a similar data type, they always yield &lt;code&gt;true&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let fruit_1 = 'apple'
let fruit_2 = 'apple'
console.log(fruit_1 === fruit_2) //Output: true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fch6dd7n7lhmuvludssla.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fch6dd7n7lhmuvludssla.gif" alt="Demonstration of the equalities of primitives with a funny meme" width="800" height="432"&gt;&lt;/a&gt;&lt;br&gt;
2) Non-Primitives and the effects of Referential Equality&lt;br&gt;
When two non-primitives of the same data type are strict-equated, the results will be surprising. Look at the following snippet where two arrays with similar data types and similar items are equated.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let basket_1 = [ 'apple', 'banana' ]
let basket_2 = [ 'apple', 'banana' ]
console.log(basket_1 === basket_2) //Output: false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgv222taf70atlnd4ywqi.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgv222taf70atlnd4ywqi.gif" alt="Demonstration of the equalities of non-primitives with a funny meme" width="800" height="432"&gt;&lt;/a&gt;&lt;br&gt;
But, when we check the equality criteria of &lt;code&gt;basket_3&lt;/code&gt; and &lt;code&gt;basket_1&lt;/code&gt;, it returns &lt;code&gt;true&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let basket_1 = [ 'apple', 'banana' ]
let basket_2 = [ 'apple', 'banana' ]
let basket_3 = basket_1
console.log(basket_3 === basket_1) //Output: true;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhxugzw1ruzwqcgtiyhcb.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhxugzw1ruzwqcgtiyhcb.gif" alt="Demonstration of the equalities of non-primitives with a funny meme" width="800" height="432"&gt;&lt;/a&gt;&lt;br&gt;
Let's try one more thing.&lt;br&gt;
When we push another item into &lt;code&gt;basket_3&lt;/code&gt;, it should not affect &lt;code&gt;basket_1&lt;/code&gt;. But it does.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let basket_1 = [ 'apple', 'banana' ]
let basket_2 = [ 'apple', 'banana' ]
let basket_3 = basket_1
basket_3.push('orange')
console.log(basket_1) //Output: [ 'apple', 'banana', 'orange' ]
console.log(basket_3) //Output: [ 'apple', 'banana', 'orange' ]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;But why is this happening?&lt;/strong&gt;&lt;br&gt;
This is where we should know about Referential equality.&lt;/p&gt;
&lt;h2&gt;
  
  
  Referential Equality
&lt;/h2&gt;

&lt;p&gt;This seems to be a Jargon, doesn't it? Actually No.&lt;br&gt;
Referential Equality is like asking, "Are these two non-primitive things pointing to the same spot in the computer's memory ?" If yes, they are referentially equal(i.e., &lt;code&gt;true&lt;/code&gt;) otherwise, they aren't(i.e., &lt;code&gt;false&lt;/code&gt;). As simple as that.&lt;br&gt;
Take a look at the following representation to get it better.&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fokep0vgoia23a3qu64mx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fokep0vgoia23a3qu64mx.png" alt="Pictorial representation of arrays occupying various memory locations though they contain the same item in them" width="448" height="335"&gt;&lt;/a&gt;&lt;br&gt;
Even though the &lt;code&gt;basket_1&lt;/code&gt;and &lt;code&gt;basket_2&lt;/code&gt;arrays have similar items, they occupy different locations in memory. When we use strict equality (===) to compare them, JavaScript checks whether they refer to the same location in memory, and since they are on distinct memory locations, the result is &lt;code&gt;false&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;But, things changed when we declared &lt;code&gt;basket_3&lt;/code&gt; and assigned it to &lt;code&gt;basket_1&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flf40xhcr2eyqaict7eb9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flf40xhcr2eyqaict7eb9.png" alt="Pictorial representation of arrays sharing the same memory location" width="616" height="346"&gt;&lt;/a&gt;&lt;br&gt;
By assigning &lt;code&gt;let basket_3 = basket_1&lt;/code&gt;, we forced the variable &lt;code&gt;basket_3&lt;/code&gt;to point to the exact memory address as &lt;code&gt;basket_1&lt;/code&gt;.  So the result was true when we did &lt;code&gt;console.log(basket_3===basket_1)&lt;/code&gt;.&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi6s8zrfqzl7w2vozf3t1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi6s8zrfqzl7w2vozf3t1.png" alt="Pictorial representation of arrays sharing the same memory location" width="616" height="346"&gt;&lt;/a&gt;&lt;br&gt;
As &lt;code&gt;basket_3&lt;/code&gt;and &lt;code&gt;basket_1&lt;/code&gt; point the same memory location, changes made through one of the variables will reflect in the other.  Therefore, when we push the item 'orange' only into &lt;code&gt;basket_3&lt;/code&gt;, we observe that the item 'orange' is now present in both arrays&lt;/p&gt;

&lt;p&gt;However, for primitive types like strings, numbers etc., we are more interested in comparing their values rather than their references. JavaScript decides whether to compare a variable by the value or by its reference based on its primitive and non-primitive nature.&lt;/p&gt;
&lt;h2&gt;
  
  
  How to work with Non-primitives?
&lt;/h2&gt;

&lt;p&gt;How do we work with them if they keep sharing their addresses?  JavaScript has nothing to do with this. Even some of the libraries like React, utilize this uniqueness of the non-primitives to control the component renders. &lt;br&gt;
But, we can prevent them from sharing memory locations or memory addresses.&lt;br&gt;
If the array is on the top level (i.e., no nesting within arrays) we can shallow copy the existing array into the other. &lt;br&gt;
&lt;strong&gt;Create a shallow copy using the spread operator (...)&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let fruit_1 = ['apple'];
let fruit_2 = ['apple'];
let fruit_3 = [...fruit_1]
fruit_3.push('orange')
console.log(fruit_3) //Output: ['apple', 'orange']
console.log(fruit_1) //Output: ['apple']
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Now you may get a question. What about the other non-primitives? Will this even work with JavaScript objects? The answer is, YES.&lt;br&gt;
Let's take a complex example of an object whose values are nested deeper than the top level.&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fisewjagrlo26jg2qfqa9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fisewjagrlo26jg2qfqa9.png" alt="Demonstration of failing copy fail due to the deeply nested object" width="800" height="300"&gt;&lt;/a&gt;&lt;br&gt;
Wait, What? The shallow copy is supposed to copy the contents into a new memory address. But in the above case, making changes in the &lt;code&gt;inventory&lt;/code&gt;object changes the &lt;code&gt;shop&lt;/code&gt;object as well. So, this is the catch with the shallow copy. You cannot use shallow copy with deeply nested objects and arrays.&lt;br&gt;
We can overcome this by using &lt;code&gt;JSON.stringify()&lt;/code&gt; and &lt;code&gt;JSON.parse()&lt;/code&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvnx8ay8izar6pr2igl4f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvnx8ay8izar6pr2igl4f.png" alt="Picture representing the deep copy of objects" width="800" height="462"&gt;&lt;/a&gt;&lt;br&gt;
According to the above representation, changes made on &lt;code&gt;inventory&lt;/code&gt;object have no impact on the &lt;code&gt;shop&lt;/code&gt;object. And this is how we perform deep copy of nested objects.&lt;br&gt;
If you are confused with JSON.parse() and JSON.stringify(), please refer to the official documentation available at the end of this article.&lt;br&gt;
Using JSON methods has its drawbacks. They can be rectified by using appropriate methods and techniques. Given the article's length, let's wrap up our discussion here and I look forward on to delving another article, discussing the alternatives to the JSON methods. Thank you for reading and have a nice day.&lt;/p&gt;



&lt;p&gt;Here are the official MDN docs on JSON.parse() and JSON.strigify() methods&lt;br&gt;
&lt;/p&gt;
&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
      &lt;div class="c-embed__cover"&gt;
        &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse" class="c-link s:max-w-50 align-middle" rel="noopener noreferrer"&gt;
          &lt;img alt="" src="https://res.cloudinary.com/practicaldev/image/fetch/s--UHjFdr30--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://developer.mozilla.org/mdn-social-share.cd6c4a5a.png" height="450" class="m-0" width="800"&gt;
        &lt;/a&gt;
      &lt;/div&gt;
    &lt;div class="c-embed__body"&gt;
      &lt;h2 class="fs-xl lh-tight"&gt;
        &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse" rel="noopener noreferrer" class="c-link"&gt;
          JSON.parse() - JavaScript | MDN
        &lt;/a&gt;
      &lt;/h2&gt;
        &lt;p class="truncate-at-3"&gt;
          The JSON.parse() static method parses a JSON string, constructing the JavaScript value or object described by the string. An optional reviver function can be provided to perform a transformation on the resulting object before it is returned.
        &lt;/p&gt;
      &lt;div class="color-secondary fs-s flex items-center"&gt;
          &lt;img alt="favicon" class="c-embed__favicon m-0 mr-2 radius-0" src="https://res.cloudinary.com/practicaldev/image/fetch/s--_bI64N_S--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://developer.mozilla.org/favicon-48x48.cbbd161b.png" width="48" height="48"&gt;
        developer.mozilla.org
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;



&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
      &lt;div class="c-embed__cover"&gt;
        &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify" class="c-link s:max-w-50 align-middle" rel="noopener noreferrer"&gt;
          &lt;img alt="" src="https://res.cloudinary.com/practicaldev/image/fetch/s--UHjFdr30--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://developer.mozilla.org/mdn-social-share.cd6c4a5a.png" height="450" class="m-0" width="800"&gt;
        &lt;/a&gt;
      &lt;/div&gt;
    &lt;div class="c-embed__body"&gt;
      &lt;h2 class="fs-xl lh-tight"&gt;
        &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify" rel="noopener noreferrer" class="c-link"&gt;
          JSON.stringify() - JavaScript | MDN
        &lt;/a&gt;
      &lt;/h2&gt;
        &lt;p class="truncate-at-3"&gt;
          The JSON.stringify() static method converts a JavaScript value to a JSON string, optionally replacing values if a replacer function is specified or optionally including only the specified properties if a replacer array is specified.
        &lt;/p&gt;
      &lt;div class="color-secondary fs-s flex items-center"&gt;
          &lt;img alt="favicon" class="c-embed__favicon m-0 mr-2 radius-0" src="https://res.cloudinary.com/practicaldev/image/fetch/s--_bI64N_S--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://developer.mozilla.org/favicon-48x48.cbbd161b.png" width="48" height="48"&gt;
        developer.mozilla.org
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;






&lt;p&gt;Follow me on X, if you wish to see my progress in becoming a full-stack Java developer.&lt;br&gt;
&lt;/p&gt;
&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
    &lt;a href="https://twitter.com/Subaashbala" rel="noopener noreferrer"&gt;
      twitter.com
    &lt;/a&gt;
&lt;/div&gt;


</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Simplify, Groupify, Conquer: The convenience of Object.GroupBy() in JavaScript</title>
      <dc:creator>Subaash</dc:creator>
      <pubDate>Sun, 17 Dec 2023 17:22:34 +0000</pubDate>
      <link>https://dev.to/subaash_b/simplify-groupify-conquer-the-convenience-of-objectgroupby-in-javascript-2h5</link>
      <guid>https://dev.to/subaash_b/simplify-groupify-conquer-the-convenience-of-objectgroupby-in-javascript-2h5</guid>
      <description>&lt;p&gt;It was a regular day of coding when I decided to revisit an old project of mine. As I scrolled through my code, I noticed a piece of code I had written during the earlier days of my JavaScript journey. The project was an Expense Tracker web application that allowed users to manually enter transactions and store them in LocalStorage. The specific code snippet I encountered was related to the grouping of data.&lt;/p&gt;

&lt;p&gt;Back then, I remember grappling with the challenges of grouping data and attempting solutions using &lt;code&gt;filter()&lt;/code&gt;, &lt;code&gt;map()&lt;/code&gt;, and even &lt;code&gt;forEach()&lt;/code&gt;. However, to my surprise, I discovered that there was an actual method designed for conveniently grouping data.&lt;/p&gt;

&lt;p&gt;A sample from the actual set of data to be handled is as follows: &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

const transacHistory = [
    { type: 'Income', desc: 'Salary', amount: '52000', date: '09/01/2023' },
    { type: 'Expense', desc: 'House rent', amount: '7500', date: '09/03/2023'},
    { type: 'Expense', desc: 'Shopping', amount: '8000', date: '09/11/2023' },
    { type: 'Income', desc: 'YouTube', amount: '3508', date: '09/12/2023' },
    { type: 'Expense', desc: 'Veggies', amount: '550', date: '9/02/2023' },
    { type: 'Expense', desc: 'Internet', amount: '2250', date: '09/11/2023' },
    { type: 'Expense', desc: 'Veggies', amount: '350', date: '9/18/2023' },
]


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

&lt;/div&gt;
&lt;p&gt;Now, I wanted to group them based on the type of transaction made ( expense or income )&lt;/p&gt;
&lt;h2&gt;
  
  
  The old-school data grouping technique
&lt;/h2&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

const groupedTransacHistory = transacHistory.reduce((acc, item)=&amp;gt;{
    if(!Object.keys(acc).includes(item.type)){
        acc[item.type] = [item]
    }
    else{
        acc[item.type].push(item)
    }
    return acc;
}, [])
console.log(groupedTransacHistory)

{*
OUTPUT:
[
  Income: [
    { type: 'Income', desc: 'Salary', amount: '52000', date: '09/01/2023' },
    { type: 'Income', desc: 'YouTube', amount: '3508', date: '09/12/2023' }
  ],
  Expense: [
    { type: 'Expense', desc: 'House rent', amount: '7500', date: '09/03/2023' },
    { type: 'Expense', desc: 'Shopping', amount: '8000', date: '09/11/2023' },
    { type: 'Expense', desc: 'Veggies', amount: '550', date: '9/02/2023' },
    { type: 'Expense', desc: 'Internet', amount: '2250', date: '09/11/2023' },
    { type: 'Expense', desc: 'Veggies', amount: '350', date: '9/18/2023' }
  ]
]
*}



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

&lt;/div&gt;
&lt;p&gt;The code to achieve the above output might seem to be a bit complex, but anyone familiar with the &lt;code&gt;filter()&lt;/code&gt; method might understand the logic. If you wish to know more about JavaScript's &lt;code&gt;filter()&lt;/code&gt; method, refer to the official MDN docs embedded at the bottom of this article.  The same logic can also be implemented using &lt;code&gt;map()&lt;/code&gt; and &lt;code&gt;forEach()&lt;/code&gt; methods as well.  As their logic is similar, I've included only the code associated with the &lt;code&gt;filter()&lt;/code&gt; method.&lt;/p&gt;
&lt;h2&gt;
  
  
  Know more about the &lt;code&gt;groupBy()&lt;/code&gt; method:
&lt;/h2&gt;

&lt;p&gt;Before letting you know the actual solution using the proposed &lt;code&gt;groupBy()&lt;/code&gt; method, let's analyse how to use that in the first place.&lt;br&gt;
The &lt;code&gt;Object.groupBy()&lt;/code&gt; method is used to categorize individual items of an iterable.  The categorization is done according to the coerced string values returned by the provided callback function.  The grouped/categorized object thus returned contains unique keys with each key corresponding to an array of related values.  Is it sound confusing? You are on the right path. Follow along to see this method in action.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;SYNTAX&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuz59zt9beqbi5ouphhuo.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuz59zt9beqbi5ouphhuo.PNG" alt="Syntax of Object.groupBy() method in JavaScript"&gt;&lt;/a&gt;&lt;br&gt;
where, &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The &lt;code&gt;iterable&lt;/code&gt;is the element to be grouped&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &lt;code&gt;callbackFunction&lt;/code&gt;is the function to be passed to execute each element in the iterable.  This function should return a value which is then coerced into a string.&lt;br&gt;
&lt;code&gt;function callbackFunction(element, index){...}&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &lt;code&gt;callbackFunction&lt;/code&gt;accepts &lt;code&gt;element&lt;/code&gt;and &lt;code&gt;index&lt;/code&gt;as its arguments&lt;br&gt;
where,&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &lt;code&gt;element&lt;/code&gt; represents the current element which is being passed to the &lt;code&gt;callbackFunction&lt;/code&gt; at the moment and the &lt;code&gt;index&lt;/code&gt; is the index of the &lt;code&gt;element&lt;/code&gt; itself.&lt;br&gt;
Let's look at some general and simple examples to grasp the concept.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Examples
&lt;/h2&gt;

&lt;p&gt;The following code groups items based on whether they start with a vowel or a consonant.&lt;/p&gt;

&lt;p&gt;&lt;u&gt;1. The traditional approach:&lt;/u&gt;&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

const vowels = ['a', 'e', 'i', 'o', 'u']
const cartItems = ['apples', 'bananas', 'cherries', 'umbrellas', 'cat food', 'pedigree', 'cupcakes']
const res2 = cartItems.reduce((accumulator, item) =&amp;gt; {
    if (vowels.includes(item.slice(0, 1))) {
        if (!accumulator['vowels']) {
            accumulator['vowels'] = [];
        }
        accumulator['vowels'].push(item);
    } else {
        if (!accumulator['non-vowels']) {
            accumulator['non-vowels'] = [];
        }
        accumulator['non-vowels'].push(item);
    }
    return accumulator;
}, {});
{*
OUTPUT: 
{
  vowels: ['apples', 'umbrellas'],
  non-vowels: ['bananas', 'cherries', 'cat food', 'pedigree', 'cupcakes']
}
*}


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

&lt;/div&gt;
&lt;p&gt;&lt;u&gt;2. The &lt;code&gt;Object.groupBy()&lt;/code&gt; approach:&lt;/u&gt;&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

const vowels = ['a', 'e', 'i', 'o', 'u']
const cartItems = ['apples', 'bananas', 'cherries', 'umbrellas', 'cat food', 'pedigree', 'cupcakes']
Object.groupBy(cartItems, (item)=&amp;gt;vowels.includes(item.slice(0, 1)))
//item.slice(0,1) returns the first letter of the currently iterated item and checks whether it is a vowel.
{*
OUTPUT: 
{
  true: ['apples', 'umbrellas'],
  false: ['bananas', 'cherries', 'cat food', 'pedigree', 'cupcakes']
}
*}


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

&lt;/div&gt;
&lt;p&gt;If there are no object properties to be displayed in the case of arrays, the &lt;code&gt;Object.groupBy()&lt;/code&gt; method simply uses the Boolean values as keys. If we need precisely set keys, then we should implement &lt;code&gt;Map.groupBy()&lt;/code&gt; method which is the replica of our former method but lets us set keys instead of the default Boolean.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

const fictionalCharacters = [
  { character: 'Ironman', ability: 'Fights bad guys' },
  { character: 'Barbie', ability: "Doesn't fight" },
  { character: 'Black widow', ability: 'Fights bad guys' },
  { character: 'Oggy', ability: "Doesn't fight" },
  { character: 'Peppa Pig', ability: "Doesn't fight" }
]
Object.groupBy( fictionalCharacters, (item)=&amp;gt;item.ability )
{* The value of item.ability is type-converted into a string and used as a key to group similar items in arrays, where the key's value is an array of corresponding items. Example: { item.ability: [{}, {}] } *}
{*
OUTPUT:
[
   Fights bad guys: [
      { character: 'Ironman', ability: 'Fights bad guys' },
      { character: 'Black widow', ability: 'Fights bad guys' },
   ],
   Doesn't fight: [
      { character: 'Barbie', ability: "Doesn't fight" },
      { character: 'Oggy', ability: "Doesn't fight" },
      { character: 'Peppa Pig', ability: "Doesn't fight" }
   ]
]
*}


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

&lt;/div&gt;
&lt;p&gt;So, as now we have a pretty decent understanding of how it works, let's get back to our expense tracker example.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

const transacHistory = [
    { type: 'Income', desc: 'Salary', amount: '52000', date: '09/01/2023' },
    { type: 'Expense', desc: 'House rent', amount: '7500', date: '09/03/2023'},
    { type: 'Expense', desc: 'Shopping', amount: '8000', date: '09/11/2023' },
    { type: 'Income', desc: 'YouTube', amount: '3508', date: '09/12/2023' },
    { type: 'Expense', desc: 'Veggies', amount: '550', date: '9/02/2023' },
    { type: 'Expense', desc: 'Internet', amount: '2250', date: '09/11/2023' },
    { type: 'Expense', desc: 'Veggies', amount: '350', date: '9/18/2023' },
]
Object.groupBy(transacHistory, (item)=&amp;gt;item.type)
{*
OUTPUT:
[
  Income: [
    { type: 'Income', desc: 'Salary', amount: '52000', date: '09/01/2023' },
    { type: 'Income', desc: 'YouTube', amount: '3508', date: '09/12/2023' }
  ],
  Expense: [
    { type: 'Expense', desc: 'House rent', amount: '7500', date: '09/03/2023' },
    { type: 'Expense', desc: 'Shopping', amount: '8000', date: '09/11/2023' },
    { type: 'Expense', desc: 'Veggies', amount: '550', date: '9/02/2023' },
    { type: 'Expense', desc: 'Internet', amount: '2250', date: '09/11/2023' },
    { type: 'Expense', desc: 'Veggies', amount: '350', date: '9/18/2023' }
  ]
]
*}


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

&lt;/div&gt;
&lt;p&gt;Simple as that. This is how the groupBy() method is convenient to use and understand. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;NOTE:&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Instead of making a huge leap to the static methods like Object.groupBy(), beginners should be comfortable using map(), filter() and reduce() methods.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The Object.groupBy() method, as of 17th Dec 2023, is not supported in all browsers and it is expected to be in the near future.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkkfkwng6pp829zxugort.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkkfkwng6pp829zxugort.PNG" alt="Browser compatibility of Object.groupBy() method"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Refer to the following document to learn more about the filter() method in JavaScript:&lt;br&gt;
&lt;/p&gt;
&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
      &lt;div class="c-embed__cover"&gt;
        &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter" class="c-link s:max-w-50 align-middle" rel="noopener noreferrer"&gt;
          &lt;img alt="" src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdeveloper.mozilla.org%2Fmdn-social-share.d893525a4fb5fb1f67a2.png" height="auto" class="m-0"&gt;
        &lt;/a&gt;
      &lt;/div&gt;
    &lt;div class="c-embed__body"&gt;
      &lt;h2 class="fs-xl lh-tight"&gt;
        &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter" rel="noopener noreferrer" class="c-link"&gt;
          Array.prototype.filter() - JavaScript | MDN
        &lt;/a&gt;
      &lt;/h2&gt;
        &lt;p class="truncate-at-3"&gt;
          The filter() method of Array instances creates a shallow copy of a portion of a given array, filtered down to just the elements from the given array that pass the test implemented by the provided function.
        &lt;/p&gt;
      &lt;div class="color-secondary fs-s flex items-center"&gt;
          &lt;img alt="favicon" class="c-embed__favicon m-0 mr-2 radius-0" src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdeveloper.mozilla.org%2Ffavicon-48x48.bc390275e955dacb2e65.png"&gt;
        developer.mozilla.org
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
To know more about Object.groupBy() method, refer to the following official docs by MDN&lt;br&gt;&lt;/p&gt;

&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
      &lt;div class="c-embed__cover"&gt;
        &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/groupBy" class="c-link s:max-w-50 align-middle" rel="noopener noreferrer"&gt;
          &lt;img alt="" src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdeveloper.mozilla.org%2Fmdn-social-share.d893525a4fb5fb1f67a2.png" height="auto" class="m-0"&gt;
        &lt;/a&gt;
      &lt;/div&gt;
    &lt;div class="c-embed__body"&gt;
      &lt;h2 class="fs-xl lh-tight"&gt;
        &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/groupBy" rel="noopener noreferrer" class="c-link"&gt;
          Object.groupBy() - JavaScript | MDN
        &lt;/a&gt;
      &lt;/h2&gt;
        &lt;p class="truncate-at-3"&gt;
          The Object.groupBy() static method groups the elements of a given iterable according to the string values returned by a provided callback function. The returned object has separate properties for each group, containing arrays with the elements in the group.
        &lt;/p&gt;
      &lt;div class="color-secondary fs-s flex items-center"&gt;
          &lt;img alt="favicon" class="c-embed__favicon m-0 mr-2 radius-0" src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdeveloper.mozilla.org%2Ffavicon-48x48.bc390275e955dacb2e65.png"&gt;
        developer.mozilla.org
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;



&lt;p&gt;Follow me along my web development journey on &lt;a href="https://twitter.com/Subaashbala" rel="noopener noreferrer"&gt;twitter/X&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>100daysofcode</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Redefine your Logic for stricter, safer JavaScript defaults with the '??' operator</title>
      <dc:creator>Subaash</dc:creator>
      <pubDate>Sun, 10 Dec 2023 15:48:46 +0000</pubDate>
      <link>https://dev.to/subaash_b/redefine-your-logic-for-stricter-safer-javascript-defaults-with-the-operator-393m</link>
      <guid>https://dev.to/subaash_b/redefine-your-logic-for-stricter-safer-javascript-defaults-with-the-operator-393m</guid>
      <description>&lt;p&gt;The title is not a clickbait. There exists an actual '??' operator in JavaScript known as the Nullish coalescing operator.  This operator accepts operands on either side and returns the right-hand side operand only when the left-hand side operand is a nullish value. This operator is absolutely a newer one and it was introduced during ES2020 to JavaScript.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;falsy&lt;/code&gt; and &lt;code&gt;nullish&lt;/code&gt;. Aren't these the same?
&lt;/h2&gt;

&lt;p&gt;No. A value is considered falsy if it coerces to &lt;code&gt;false&lt;/code&gt; when evaluated in a boolean expression. In this regard, the following values are considered to be falsy&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;null - Absence of any object value or a direct null is falsy
undefined - Variable that hasn't assigned any value is falsy
false - The boolean false is falsy
0 - The integer 0 is falsy
-0 - The integer negative 0 is falsy
"" - An empty string is falsy
NaN - Not a Number is falsy
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;u&gt;So, what is considered nullish in JavaScript?&lt;/u&gt;. Values that are either &lt;code&gt;null&lt;/code&gt; or &lt;code&gt;undefined&lt;/code&gt; are nullish in JavaScript.&lt;/p&gt;

&lt;p&gt;Before getting hands-on with this operator, we should understand the working of short circuit operators.&lt;/p&gt;
&lt;h2&gt;
  
  
  Short circuit operators
&lt;/h2&gt;

&lt;p&gt;Short circuit operators are basically 'Logical AND' &lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt; and 'Logical OR' &lt;code&gt;||&lt;/code&gt; operators. They are used to perform concise conditional logic.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&amp;amp;&amp;amp; operator&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Syntax: &lt;code&gt;expression1 &amp;amp;&amp;amp; expression2&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;returns expression1 if it is falsy and expression2 is not evaluated at all&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;returns expression2 if expression1 is truthy(anything other than a falsy value)&lt;br&gt;
Take a look at this example&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Case when the '&amp;amp;&amp;amp;' returns expression1
const number = 0 &amp;amp;&amp;amp; 100
console.log(number) //0 because number1 i.e., expression1 was falsy and JavaScript never had a chance to execute expression2
---------------------------------------------------------------------------------------------------------------------------------
//Case when '&amp;amp;&amp;amp;' returns expression2
const heroes = 'IronMan' &amp;amp;&amp;amp; 'CaptainAmerica'
console.log(heroes) //'CaptainAmerica' As string1 which is on the left hand side of the &amp;amp;&amp;amp; operator is true, it has returned string2 from the right hand side.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;blockquote&gt;
&lt;p&gt;|| operator&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Syntax: &lt;code&gt;expression1 || expression2&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;returns expression1 if expression1 is truthy&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;returns expression2 if expression1 is falsy&lt;br&gt;
Simple as that.&lt;br&gt;
Example:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//Case when expression1 is being returned
const heroes = 'IronMan' || 'CaptainAmerica'
console.log(heroes) // 'IronMan' because 'IronMan' is a truthy value
------------------------------------------------------------------------
//Case when expression2 is being returned
const number = 0 || 100
console.log(number) //100 because Number 0 is a falsy value
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h2&gt;
  
  
  The '??' operator
&lt;/h2&gt;

&lt;p&gt;In addition to the &lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt; and &lt;code&gt;||&lt;/code&gt; there exists another operator called as the Nullish Coalescing Operator. This operator is moreover, like a special case of the logical &lt;code&gt;OR&lt;/code&gt; operator and its operation is similar to that of the &lt;code&gt;||&lt;/code&gt; operator.&lt;br&gt;&lt;br&gt;
As the &lt;code&gt;||&lt;/code&gt; operator returns the right-hand side expression when the left hand side is &lt;code&gt;falsy&lt;/code&gt;, the Nullish Coalescing operator &lt;code&gt;??&lt;/code&gt; returns the right-hand side expression only when the left-hand side operand is either &lt;code&gt;null&lt;/code&gt; or &lt;code&gt;undefined&lt;/code&gt;.  So, the &lt;code&gt;??&lt;/code&gt; operator is more strict than the existing &lt;code&gt;||&lt;/code&gt; operator. &lt;br&gt;
Syntax: &lt;code&gt;expression1 ?? expression2&lt;/code&gt;&lt;br&gt;
The &lt;code&gt;??&lt;/code&gt; operator returns &lt;code&gt;expression2&lt;/code&gt; only if expression1 is either &lt;code&gt;null&lt;/code&gt; or &lt;code&gt;undefined&lt;/code&gt;.  Meanwhile, &lt;code&gt;expression1&lt;/code&gt; is returned if it is simply any value other than a nullish value.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//Case 1
const number = 0 ?? 100
console.log(number) //returns '0' because it is not nullish
------------------------------------------------------------
//Case 2
let fruit = ''
let veggie = 'Carrot'
const basket = fruit ?? veggie
console.log(basket) // '' because an empty string('') is not nullish but falsy
------------------------------------------------------------------------------------
//Case 3
let halves = 'fullPortion' / 2
let full = 1
const portion = halves ?? full
console.log(portion) // NaN. Obtained by the evaluation of the left hand side expression and it is not nullish but falsy.
----------------------------------------------------------------------------------------------------------------------------
//Case 4
let hero1
let hero2 = 'CaptainAmerica'
const heroes = hero1 ?? hero2
console.log(heroes) // 'CaptainAmerica' because 'hero1' is undefined

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

&lt;/div&gt;


&lt;p&gt;The &lt;code&gt;??&lt;/code&gt; operator is designed in such a way that it cannot be used along with the &lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt; and &lt;code&gt;||&lt;/code&gt; without proper brackets.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;null || undefined ?? 'hi' // SyntaxError: missing ) after argument list
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(null || undefined) ?? 'hi' //'hi'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h2&gt;
  
  
  But why them?
&lt;/h2&gt;

&lt;p&gt;This question arises as soon as we see some unfamiliar syntax that makes some work. Can't we just stick with the classic &lt;code&gt;if else&lt;/code&gt; statement to quantify expressions? Yes we can, but No. Let me explain why is that.&lt;br&gt;
We can use &lt;code&gt;if else&lt;/code&gt; conditions as long as we use plain JavaScript.  But when it comes to using frontend UI libraries like ReactJs, it is impossible to embed 'if statements' within the JSX structure. We should use only expressions that use logical operators in this regard. Confused about expressions and statements? Scroll down to the bottom and you can find the relevant resource.&lt;br&gt;&lt;br&gt;
This is not the only use case.  But these short circuit operators including the nullish coalescing operator avoids unnecessary execution of expressions with a single line of code.&lt;br&gt;&lt;br&gt;
You are open to construct a more complex logic using short-circuit operators as follows:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const gameConfig1 = { 
 playerName: 'John',
 score: 22500,
 highScore: 35000,
}
const gameConfig2 = { 
 playerName: undefined, 
 score: undefined,
 highScore: undefined
}
function getPlayerInfo( player ){
 const { playerName, score, highScore } = player
 const currentPlayerName= playerName ?? 'Guest'
 const currentPlayerScore = ( score &amp;gt;= 100 &amp;amp;&amp;amp; score ) || 'No scores yet'
 const currentPlayerHighScore = highScore ?? 'No high scores yet'
 return { currentPlayerName, currentPlayerScore , currentPlayerHighScore }
}
const player1 = getPlayerInfo( gameConfig1 ) // { name: John, score: 22500, highScore: 35000 }
const player2 = getPlayerInfo( gameConfig2 ) // { name: 'Guest', score: 'No scores yet', highScore: 'No high scores yet' }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;That's all about the Nullish coalescing operator. Practice more to get familiar with this operator. It may take some to do so, but it's worth the time you spend. &lt;br&gt;
Following is the article distinguishing between Expression and Statement in JavaScript&lt;br&gt;
&lt;/p&gt;
&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
      &lt;div class="c-embed__cover"&gt;
        &lt;a href="https://www.freecodecamp.org/news/statement-vs-expression-whats-the-difference-in-programming/#:~:text=The%20Main%20Differences%20Between%20an,values%20or%20execute%20to%20values." class="c-link s:max-w-50 align-middle" rel="noopener noreferrer"&gt;
          &lt;img alt="" src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.freecodecamp.org%2Fnews%2Fcontent%2Fimages%2F2022%2F12%2Fkaleidico-7lryofJ0H9s-unsplash.jpg" height="auto" class="m-0"&gt;
        &lt;/a&gt;
      &lt;/div&gt;
    &lt;div class="c-embed__body"&gt;
      &lt;h2 class="fs-xl lh-tight"&gt;
        &lt;a href="https://www.freecodecamp.org/news/statement-vs-expression-whats-the-difference-in-programming/#:~:text=The%20Main%20Differences%20Between%20an,values%20or%20execute%20to%20values." rel="noopener noreferrer" class="c-link"&gt;
          Statement vs Expression – What's the Difference in Programming?
        &lt;/a&gt;
      &lt;/h2&gt;
        &lt;p class="truncate-at-3"&gt;
          By Ogundiran Ayobami Learning the syntax of a programming language is key if you want to use that language effectively. This is true for both new and experienced developers. And one of the most important things to pay attention to while learning a pr...
        &lt;/p&gt;
      &lt;div class="color-secondary fs-s flex items-center"&gt;
          &lt;img alt="favicon" class="c-embed__favicon m-0 mr-2 radius-0" src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.freecodecamp.org%2Funiversal%2Ffavicons%2Ffavicon.ico"&gt;
        freecodecamp.org
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
To know more about the nullish coalescing operator, refer to the following docs by MDN.&lt;br&gt;&lt;/p&gt;

&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
      &lt;div class="c-embed__cover"&gt;
        &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing#syntax" class="c-link s:max-w-50 align-middle" rel="noopener noreferrer"&gt;
          &lt;img alt="" src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdeveloper.mozilla.org%2Fmdn-social-share.d893525a4fb5fb1f67a2.png" height="auto" class="m-0"&gt;
        &lt;/a&gt;
      &lt;/div&gt;
    &lt;div class="c-embed__body"&gt;
      &lt;h2 class="fs-xl lh-tight"&gt;
        &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing#syntax" rel="noopener noreferrer" class="c-link"&gt;
          Nullish coalescing operator (??) - JavaScript | MDN
        &lt;/a&gt;
      &lt;/h2&gt;
        &lt;p class="truncate-at-3"&gt;
          The nullish coalescing (??) operator is a logical
  operator that returns its right-hand side operand when its left-hand side operand is
  null or undefined, and otherwise returns its left-hand side
  operand.
        &lt;/p&gt;
      &lt;div class="color-secondary fs-s flex items-center"&gt;
          &lt;img alt="favicon" class="c-embed__favicon m-0 mr-2 radius-0" src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdeveloper.mozilla.org%2Ffavicon-48x48.bc390275e955dacb2e65.png"&gt;
        developer.mozilla.org
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;



</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>100daysofcode</category>
      <category>beginners</category>
    </item>
    <item>
      <title>7 Amazing use cases of Array.from() method in JavaScript</title>
      <dc:creator>Subaash</dc:creator>
      <pubDate>Tue, 05 Dec 2023 06:04:40 +0000</pubDate>
      <link>https://dev.to/subaash_b/7-amazing-use-cases-of-arrayfrom-method-in-javascript-50l</link>
      <guid>https://dev.to/subaash_b/7-amazing-use-cases-of-arrayfrom-method-in-javascript-50l</guid>
      <description>&lt;p&gt;The Array.from() method creates a shallow-copied array instance from an array like object or an iterable.  The iterable object could be an array, a string, a set, an object or anything with an iterable property. &lt;/p&gt;

&lt;h2&gt;
  
  
  Syntax:
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;Array.from(iterable, mapFunction, thisArg)&lt;/code&gt;&lt;br&gt;
where, &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;iterable: the object to convert into an array&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;mapFunction (optional): A function to call on each element of the array. The mapFunction accepts two parameters as any regular Javascript map function does. The first parameter is the current element and the second one its index in the array.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;thisArg (optional): An object to pass as this when using the map function.&lt;br&gt;
Let's look at their use cases in brief&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  1. Generate numbers up to a specific range:
&lt;/h2&gt;

&lt;p&gt;The traditional method of generating numbers up to a specific range is always to use loops. But the Array.from() method is a one-liner which does the same job as the Javascript loops in this case.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// The traditional method: 
const numberCount = [];
for( let i=0; i &amp;lt; 5; i++){
    numberCount.push( i+1 )
}
console.log(numberCount) // Output: [ 1, 2, 3, 4, 5 ]

// Using Array.from() method to do the same job: 
Array.from( { length: 5 }, (_, index)=&amp;gt; index+1 )
// Output: [ 1, 2, 3, 4, 5 ]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  2. Creating a Matrix:
&lt;/h2&gt;

&lt;p&gt;While working with mathematical concepts or engaging in complex use cases, dealing with multi-dimensional arrays can be a cumbersome task, especially during the creation phase.  To create one, we use nested &lt;code&gt;for&lt;/code&gt; loops, and a small mistake in the logic can end up in vain. But the task is made easier with Array.from() method and it is easy to debug as well.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Create a grid of Default value
let defaultValue = 0;
let grid = Array.from({ length: 3 }, () =&amp;gt; Array.from({ length: 3 }, () =&amp;gt; defaultValue ));
// Output: [ [ 0, 0, 0 ], [ 0, 0, 0 ], [ 0, 0, 0 ] ]
// Create a grid with numbers in the increasing order
let grid = Array.from({ length: 3 }, () =&amp;gt; Array.from({ length: 3 }, (_, i) =&amp;gt; i+1));
// Output: [ [ 1, 2, 3 ], [ 1, 2, 3 ], [ 1, 2, 3 ] ]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  3. Mapping object properties into arrays
&lt;/h2&gt;

&lt;p&gt;If you wish to map either the keys or values of an object into an array, it is better to use &lt;code&gt;Array.from()&lt;/code&gt; in addition to &lt;code&gt;Object&lt;/code&gt;methods&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const user = { name: 'Ram', age: 23, city: 'Chennai' }
const userInfo = Array.from( Object.values(user) )
// Output: [ 'Ram', 23, 'Chennai' ]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  4. Creating an HTML nodelist
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let listItems = Array.from( { length: 5 }, (item, index)=&amp;gt;{
    const item = document.createElement('li');
    item.textContent = index + 1;
    return item
})
// Output: NodeList [ &amp;lt;li&amp;gt;, &amp;lt;li&amp;gt;, &amp;lt;li&amp;gt;, &amp;lt;li&amp;gt;, &amp;lt;li&amp;gt; ]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  5. Converting NodeList into an Array
&lt;/h2&gt;

&lt;p&gt;In addition to the previous use case at 4, the DOM items could be converted back into arrays using &lt;code&gt;Array.from()&lt;/code&gt; method&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const listItems = document.querySelectorAll('.listItems')
const listItemsArray = Array.from( listItems )
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  6. Customizing array elements
&lt;/h2&gt;

&lt;p&gt;Customizing array elements is made easier with &lt;code&gt;Array.from()&lt;/code&gt; javascript method. The Array.from() method creates a shallow copy of the array being passed and manipulates each element while creating a new array. Let's get it clear from the following example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const numbers = [ 1, 2, 3, 4, 5 ]
const multiplyByTwo = Array.from( numbers, (number)=&amp;gt;number*2 )
// Output: [ 2, 4, 6, 8, 10 ]

// Strings can be manipulated as well
const tags = [ webDev, grateful, mentorship, frontend ] 
const twitterTags = Array.from( tags, ( tag, index )=&amp;gt;'#'+tag )
//Output: [ '#webDev', '#grateful', '#mentorship', '#frontend' ]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  7. Creating a character frequency map
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;Array.from()&lt;/code&gt; can be used to find how frequently a letter occurs in a sentence or in a given passage. We could achieve this by simply utilising some basic JavaScript functions and methods.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const greet = 'Hello, world!';
const freqMap = Array.from( greet, ( letter )=&amp;gt;({[letter]: greet.split( letter ).length-1 }))
// Output: 
[
  { H: 1 },   { e: 1 },
  { l: 3 },   { l: 3 },
  { o: 2 },   { ',': 1 },
  { ' ': 1 }, { w: 1 },
  { o: 2 },   { r: 1 },
  { l: 3 },   { d: 1 },
  { '!': 1 }
]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These are some use cases of JavaScript's Array.from() method. Please note that, some of the use cases could efficiently be solved using alternate methods available within Javascript, though I wanted to showcase the abilities of this method.  For details about the basics and more basic examples, visit the official &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from"&gt;MDN&lt;/a&gt; docs page.&lt;/p&gt;

</description>
      <category>array</category>
      <category>method</category>
      <category>javascript</category>
      <category>iterator</category>
    </item>
    <item>
      <title>Exploring the unusual: JavaScript arrays and the 'in' operator</title>
      <dc:creator>Subaash</dc:creator>
      <pubDate>Fri, 24 Nov 2023 13:30:28 +0000</pubDate>
      <link>https://dev.to/subaash_b/exploring-the-unusual-javascript-arrays-and-the-in-operator-jlh</link>
      <guid>https://dev.to/subaash_b/exploring-the-unusual-javascript-arrays-and-the-in-operator-jlh</guid>
      <description>&lt;h2&gt;
  
  
  The 'in' operator of JavaScript
&lt;/h2&gt;

&lt;p&gt;JavaScript is one of the weirdest programming languages both syntactically and by use case. JavaScript is widely used to create fullstack websites, cross-platform mobile applications and even desktop applications. Inspite of it wide usage, JavaScript with its different features can make the developer go nuts.  One such thing is the 'in' operator.&lt;/p&gt;

&lt;p&gt;In other programming languages like Python, the 'in' keyword directly represents its semantic meaning, i.e., usage of 'in' keyword checks for the presence of an item in an iterable.  Whereas in Javascript, the 'in' operator checks for the presence of a particular property in an object. Doesn't sound strange right? The peculiarity becomes even more evident when using 'in' operator with arrays.&lt;/p&gt;

&lt;h2&gt;
  
  
  'in' operator with JavaScript objects
&lt;/h2&gt;

&lt;p&gt;As mentioned earlier, the 'in' operator is used to check for the presence of a property in a JavaScript object. &lt;br&gt;
For example,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const shoppingCart = { 'apple': 1, 'banana': 5, 'pears': 3 }
console.log( 'apple' in shoppingCart ) //Output: true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The above code is said to return true. But what if it was used in an array?&lt;/p&gt;
&lt;h2&gt;
  
  
  'in' operator with JavaScript arrays
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const shoppingCart = [ 'apple', 'banana', 'pears' ] 
console.log( 'apple' in shoppingCart ) //Output: false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Semantically, it should return true instead, it returns false. Why is this happening ?  Behind the hood, Javascript's 'in' operator checks for the existence of an index and so the unexpected answer.  Let's see something more weird.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const shoppingCart = [ 'apple', 'banana', 'pears' ]
console.log( '1' in shoppingCart ) //Output: true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The above snippet returns true, because the 'in' operator checks for the presence of an element at the '1'st index instead of checking for the presence of an actual '1' in the array. Let's see what happens under the hood in JavaScript arrays.&lt;/p&gt;
&lt;h2&gt;
  
  
  Behind the scenes of JavaScript arrays
&lt;/h2&gt;

&lt;p&gt;Arrays are interpreted as objects by JavaScript. The _shoppingCart _array defined above is interpreted by JavaScript as follows:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const shoppingCart = { 0: 'apple', 1: 'banana', 2: 'pears', length: 3}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;This clearly explains the behaviour of the 'in' operator.  So, it checks whether the '1'st index is actually not undefined. If it does, it returns true else false.  But why do someone dare to use it with the JavaScript arrays? The answer is, 'in' operator with the JavaScript arrays checks for the presence of a particular index. If you wish to learn more about the 'in' operator, check out the official docs on MDN: &lt;/p&gt;
&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
      &lt;div class="c-embed__cover"&gt;
        &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/in" class="c-link s:max-w-50 align-middle" rel="noopener noreferrer"&gt;
          &lt;img alt="" src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdeveloper.mozilla.org%2Fmdn-social-share.d893525a4fb5fb1f67a2.png" height="auto" class="m-0"&gt;
        &lt;/a&gt;
      &lt;/div&gt;
    &lt;div class="c-embed__body"&gt;
      &lt;h2 class="fs-xl lh-tight"&gt;
        &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/in" rel="noopener noreferrer" class="c-link"&gt;
          in - JavaScript | MDN
        &lt;/a&gt;
      &lt;/h2&gt;
        &lt;p class="truncate-at-3"&gt;
          The in operator returns true if the specified property is in the specified object or its prototype chain.
        &lt;/p&gt;
      &lt;div class="color-secondary fs-s flex items-center"&gt;
          &lt;img alt="favicon" class="c-embed__favicon m-0 mr-2 radius-0" src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdeveloper.mozilla.org%2Ffavicon-48x48.bc390275e955dacb2e65.png"&gt;
        developer.mozilla.org
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;



</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>in</category>
      <category>operators</category>
    </item>
  </channel>
</rss>
