<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Shahid's Blog]]></title><description><![CDATA[Shahid's Blog]]></description><link>https://blog.sidmirza.com</link><generator>RSS for Node</generator><lastBuildDate>Wed, 29 Apr 2026 21:44:52 GMT</lastBuildDate><atom:link href="https://blog.sidmirza.com/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[5 mistakes I made as a beginner developer 🤯.]]></title><description><![CDATA[Today I wanted to look back at the time when I decided to start learning to code, changing my career from being a civil engineer to a software developer.It was the best decision of my life, but as a beginner, I made a lot of mistakes.I want to highli...]]></description><link>https://blog.sidmirza.com/5-mistakes-i-made-as-a-beginner-developer</link><guid isPermaLink="true">https://blog.sidmirza.com/5-mistakes-i-made-as-a-beginner-developer</guid><category><![CDATA[Beginner Developers]]></category><category><![CDATA[Web Development]]></category><category><![CDATA[JavaScript]]></category><dc:creator><![CDATA[Mohd Shahid]]></dc:creator><pubDate>Wed, 11 May 2022 13:31:00 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1652275542283/lBU3R5WsT.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Today I wanted to look back at the time when I decided to start learning to code, changing my career from being a civil engineer to a software developer.<br />It was the best decision of my life, but as a beginner, I made a lot of mistakes.<br />I want to highlight some of them so that others can avoid the mistakes I made.  </p>
<hr />
<h3 id="heading-1-be-stuck-in-tutorial-hell">1. Be stuck in tutorial hell</h3>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t2x4w7gq8m73nnt89234.png" alt="Tutorial hell" /></p>
<p>This was the worst mistake, when I started I just wanted to watch courses and read articles without actually using the knowledge I was grasping.<br />This went on for about a year but when I realized that making projects was as important as watching lectures, I started making projects until then I had already forgotten most of the concepts, so I had to re-watch the videos every time I needed to refresh some concept. </p>
<blockquote>
<p>Start practicing as soon as you learn something new. </p>
</blockquote>
<hr />
<h3 id="heading-2-imposter-syndrome">2. Imposter syndrome</h3>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nzjhmr36npnjib25qb38.png" alt="Imposter syndrome " /></p>
<p>When I finally started using the knowledge I gained, making projects, I was getting lots of errors and bugs -at that time I did not know it was normal, so I used to get discouraged. I started to think that coding was not for me, and I do not have enough knowledge to complete the project I started. </p>
<blockquote>
<p>Never think that you cannot do it.</p>
</blockquote>
<h3 id="heading-3-be-a-lone-wolf">3. Be a lone wolf</h3>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wv9truk9yrhw9lqwhy94.png" alt="Lone wolf" /> 
Whenever I was stuck and needed help I did not ask for it, I felt stupid that I was asking such a dumb question. I wanted to learn everything on my own, which is not possible. So please do not hesitate when asking for help, no matter how dumb a question you have, there are a lot of great people out there ready to help.</p>
<blockquote>
<p>Be a team player.</p>
</blockquote>
<hr />
<h3 id="heading-4-not-applying-for-a-job-sooner">4. Not applying for a job sooner</h3>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/te2u66cdsbwwvplrc5um.png" alt="Job" /></p>
<p>I did not apply for a job even after learning to code and making lots of projects, I always thought I was not a good fit for the market and that there is a lot of competition out there, and I wouldn't get a chance. But later I realized that one can land a job right after learning HTML and CSS.</p>
<hr />
<h3 id="heading-5-not-building-an-online-presence">5. Not building an online presence</h3>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1bonxo7332lkam8vrb4e.png" alt="Online presence" /></p>
<p>Until a few months back no one knew that I was a software developer. I started using <a target="_blank" href="https://twitter.com/sidmirza4">Twitter</a> recently and I am so glad that I did. You can get lots of opportunities using online platforms like Twitter, Instagram, Dev.to and Hashnode, etc.</p>
<blockquote>
<p>Build an online presence.</p>
</blockquote>
<hr />
<p>Thanks a lot for reading. 💖</p>
<p>Please follow me on <a target="_blank" href="https://twitter.com/sidmirza4">Twitter</a></p>
]]></content:encoded></item><item><title><![CDATA[Interviewer's Favorite: Event Bubbling]]></title><description><![CDATA[Hello everyone 👋
It's been a long time since I published my last article. I got busy in learning deployment, AWS, Docker, Kubernetes, etcetera, which I will share here as well, so be tuned! 👀
Today's article is gonna be about 'events', more specifi...]]></description><link>https://blog.sidmirza.com/interviewers-favorite-event-bubbling</link><guid isPermaLink="true">https://blog.sidmirza.com/interviewers-favorite-event-bubbling</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[#codenewbies]]></category><category><![CDATA[Programming Blogs]]></category><category><![CDATA[React]]></category><category><![CDATA[Developer]]></category><dc:creator><![CDATA[Mohd Shahid]]></dc:creator><pubDate>Sat, 15 Jan 2022 10:13:28 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1642241140834/6enD6Lnac.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Hello everyone 👋</p>
<p>It's been a long time since I published my last article. I got busy in learning deployment, AWS, Docker, Kubernetes, etcetera, which I will share here as well, so be tuned! 👀</p>
<p>Today's article is gonna be about 'events', more specifically 'event bubbling'.</p>
<h2 id="heading-lets-get-started">Let's get started 🚀</h2>
<p>Before starting the article I want you to familiarize with the word 'event' in JS. Those who are already familiar can skip the introduction section.  </p>
<h2 id="heading-introduction">Introduction</h2>
<h3 id="heading-what-is-an-event">What is an event 🤔?</h3>
<p>One of the most important questions that an interviewer asks around JS and DOM, is about 'events'.
When you start learning JS and playing with DOM (Document Object Model), you may hear/read the word 'event', so what is it?<br />In simple language when a user interact with the webpage in any way like click of a mouse button (left or right), pressing a keyboard button, form submission etc. These interactions are called 'events'.</p>
<h3 id="heading-why-events-are-important">Why 'events' are important 💭?</h3>
<p>If you want to become a better front-end developer or a web developer in general, you really need to understand behind the scenes of what happens when user interacts with the web page.<br />Almost all the questions in the interview about DOM revolves around 'events'.<br />Now, when a user do something on the webpage that causes an event to happen you can capture that event and do some specific task accordingly. </p>
<h3 id="heading-what-is-event-bubbling">What is 'Event Bubbling' 🙄?</h3>
<p>With 'event' you will hear/read some more terms like 'Event Capturing', 'Event Propagation', and 'Event Bubbling'. Well, as you already know this article is gonna be about 'event bubbling', which is, without a doubt, is the most important.<br />Consider the following code snippet.  👇</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<p>In the above example if you click on an <code>li</code> element, it will occur as if the click happened on its parent <code>ul</code> and all the other parents above it.<br />This is because the event bubbles up from the element it happened to all of its parents. It is called event bubbling.<br />Confusing 😵 right?<br />To visualize this I am creating a simple web page and attaching event listeners to <code>div</code>, <code>ul</code> and <code>li</code>.  </p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"list__container"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">ul</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"list"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"list__item"</span>&gt;</span>List item 1<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"list__item"</span>&gt;</span>List item 2<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>                     
    <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"list__item"</span>&gt;</span>List item 3<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<p>This is how it would look in in the inspector 👇  </p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u3ut8dgzyo698x3whcet.png" alt="event-bubbling-web-page" /></p>
<p>Now the magic of JS begins 💥</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> listContainer = <span class="hljs-built_in">document</span>.querySelector(<span class="hljs-string">'.list__container'</span>);
<span class="hljs-keyword">const</span> list = <span class="hljs-built_in">document</span>.querySelector(<span class="hljs-string">'.list'</span>);
<span class="hljs-keyword">const</span> listItems = <span class="hljs-built_in">document</span>.querySelectorAll(<span class="hljs-string">'.list__item'</span>);

<span class="hljs-comment">// on click changing the item's background color to be red.</span>
listItems.forEach(<span class="hljs-function">(<span class="hljs-params">listItem</span>) =&gt;</span> {
  listItem.addEventListener(<span class="hljs-string">'click'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">event</span>) </span>{
    <span class="hljs-comment">// `this` referes to the `li` element which was clicked.</span>
    <span class="hljs-built_in">this</span>.style.backgroundColor = <span class="hljs-string">'red'</span>;
   });
});
</code></pre>
<p>The result 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bo2ypvtzf82v18f89dg9.gif" alt="click-on-list" /></p>
<p>Everything is working as expected right 👀?
...
Now let's add event listeners to <code>div</code> and <code>ul</code>. 👇</p>
<pre><code class="lang-js"><span class="hljs-comment">// click event to `div`</span>
listContainer.addEventListener(<span class="hljs-string">'click'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">event</span>) </span>{
    <span class="hljs-built_in">this</span>.style.backgroundColor = <span class="hljs-string">'yellow'</span>;
});

<span class="hljs-comment">// click event on `ul`</span>
list.addEventListener(<span class="hljs-string">'click'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">event</span>) </span>{
    <span class="hljs-built_in">this</span>.style.backgroundColor = <span class="hljs-string">'green'</span>;
});
</code></pre>
<p>The result 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2kr9ytrcazoruuf088oj.gif" alt="change-color-ul-div" /></p>
<p>🤯 I know you might be thinking, 'Woah, wait Shahid, you never clicked on <code>ul</code> or <code>div</code> why the hell their background color changed 🤔?'<br />The answer is: 'Event Bubbling'.<br />When you click an element the event is bubbled up to all of its parent and it seems that the click is happened on each of its parent.<br />We can make some cool shit using the power of event bubbling, but that is out of the scope of this article.<br />If you want me to write more (and show more example) on event bubbling, please leave a comment below ⬇️.<br />Thanks a lot for reading this article. 💓<br />Follow me <a target="_blank" href="https://twitter.com/sidmirza4">twitter</a> to show some love ❣️</p>
]]></content:encoded></item><item><title><![CDATA[Click to Copy!]]></title><description><![CDATA[Hey Guys 👋
Ever wondered how some websites have "click to copy" functionality?  

You can also do just that, whether you're building an e-commerce website where you want user to click on share button to write the url of the product on their website....]]></description><link>https://blog.sidmirza.com/click-to-copy</link><guid isPermaLink="true">https://blog.sidmirza.com/click-to-copy</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[Web Development]]></category><dc:creator><![CDATA[Mohd Shahid]]></dc:creator><pubDate>Sat, 28 Aug 2021 10:43:54 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1630147306196/pK-BptOhM.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h3 id="hey-guys">Hey Guys 👋</h3>
<p>Ever wondered how some websites have "click to copy" functionality?  </p>
<ul>
<li>You can also do just that, whether you're building an e-commerce website where you want user to click on share button to write the url of the product on their website.  </li>
<li>or you are building some website/documentation where people can click somewhere and copy the code snippet.</li>
</ul>
<p>In this article I will explain how you can mess around with the user's clipboard 😄. </p>
<h2 id="lets-get-started">Let's get started 🚀</h2>
<p>To explain this better, I will create a demo project. You can  follow along if you want to.  </p>
<ul>
<li>Make a folder and name it whatever you like.  </li>
<li>In that folder make two files<ul>
<li><ol>
<li><code>index.html</code>    </li>
</ol>
</li>
<li><ol>
<li><code>app.js</code>  </li>
</ol>
</li>
</ul>
</li>
</ul>
<p>Add the following HTML code in <code>index.html</code>.  </p>
<pre><code class="lang-html"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-meta-keyword">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">"en"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">"UTF-8"</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">http-equiv</span>=<span class="hljs-string">"X-UA-Compatible"</span> <span class="hljs-attr">content</span>=<span class="hljs-string">"IE=edge"</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"viewport"</span> <span class="hljs-attr">content</span>=<span class="hljs-string">"width=device-width, initial-scale=1.0"</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Copy to Clipboard 📋<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">style</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">style</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">h2</span>&gt;</span>Implementing copy to clipboard with a simple click 📋<span class="hljs-tag">&lt;/<span class="hljs-name">h2</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">button</span>&gt;</span>Click to copy<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"./app.js"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<ul>
<li>Now select <code>h2</code> and <code>button</code> element in <code>app.js</code>:<pre><code class="lang-js"><span class="hljs-keyword">const</span> h2 = <span class="hljs-built_in">document</span>.querySelector(<span class="hljs-string">'h2'</span>);
<span class="hljs-keyword">const</span> button = <span class="hljs-built_in">document</span>.querySelector(<span class="hljs-string">'button'</span>);
</code></pre>
</li>
</ul>
<p>Preview the <code>index.html</code> in your browser.</p>
<blockquote>
<p>Side note: This demo will not work in IE  </p>
</blockquote>
<p>It should look something like this 👇
<img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rhqcmdm6uu6um171yeyc.png" alt="preview of `index.html`" /></p>
<hr />

<p>Now to alter the user's clipboard we have to use <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/API/Navigator/clipboard">Clipboard API</a>.<br />The <code>clipboard</code> object is exposed by the <code>navigator.clipboard</code> property.  </p>
<p>Add the following code in the <code>app.js</code> file:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> cb = navigator.clipboard;
</code></pre>
<ul>
<li><code>console.log(cb)</code> will look something like this 👇  </li>
</ul>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nia59dvsjbkxi19goq5u.png" alt="Preview of clipboard" />  </p>
<p>👉 Remember: Clipboard API is asnychronous, means every method will return a promise which will resolve or reject.
To consume the <code>promise</code> we can either use <code>async/await</code> syntax or <code>.then/.catch</code>.  </p>
<p></p><hr /><p></p>
<h3 id="reading-text-from-clipboard">Reading text from clipboard 📋</h3>
<ul>
<li>To read the text from clipboard I will add a <code>click</code> event listener to the <code>button</code>:  </li>
<li>In the event listener I will call a method called <code>readText()</code>, which is available on the <code>cb</code> object we accessed earlier:<pre><code class="lang-js">button.addEventListener(<span class="hljs-string">'click'</span>, <span class="hljs-keyword">async</span> () =&gt; {
<span class="hljs-keyword">const</span> text = <span class="hljs-keyword">await</span> cb.readText();
<span class="hljs-built_in">console</span>.log(text);
  <span class="hljs-comment">// output will be whatever you last copied, </span>
  <span class="hljs-comment">// or empty string if the</span>
  <span class="hljs-comment">// copied data is not of type text.</span>
});
</code></pre>
</li>
<li>To read the text from clipboard user must grant <code>read</code> permission, otherwise the operation will fail. 👇<br /><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0gwby69z2pfzdaslwke7.png" alt="clipboard_permissrion" />  </li>
<li>If permission is denied 👇
<img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9mjrfhwop3mwmgebgqoj.png" alt="permission_denied" />  </li>
</ul>
<blockquote>
<p>You check if the user granted the permission or not using the <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/API/Permissions_API">Permission API</a>.  </p>
</blockquote>
<hr />

<h3 id="writing-text-to-clipboard">Writing text to clipboard 📋</h3>
<ul>
<li>To write the text to user's clipboard we use <code>writeText()</code> method available on the <code>cb</code> object.  </li>
<li>Writing permission is given automatically by the browser.  </li>
</ul>
<p>Write the following the code in the <code>app.js</code>:</p>
<pre><code class="lang-js">button.addEventListener(<span class="hljs-string">'click'</span>, <span class="hljs-keyword">async</span> () =&gt; {
  <span class="hljs-comment">// const text = await cb.readText();</span>
  <span class="hljs-comment">// console.log(text);</span>

  <span class="hljs-comment">// writing the inner text of the h2 element</span>
  <span class="hljs-comment">// to the user's clipboard.  </span>
    cb.writeText(h2.innerText).then(<span class="hljs-function">() =&gt;</span> {
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">'text written to clipboard'</span>)
    })
});
</code></pre>
<hr />
<p>That's it for this article guys.  </p>
<p>See, how easy it is you mess with the user's clipboard 😂.  </p>
<h4 id="thanks-for-giving-it-a-read-i-hope-you-found-it-useful">Thanks ❤️ for giving it a read, I hope you found it useful.</h4>
<h4 id="for-concise-tips-and-tricks-please-follow-me-on-twitterhttpswwwtwittercomsidmirza4">For concise tips and tricks, please follow me on <a target="_blank" href="https://www.twitter.com/sidmirza4">Twitter</a></h4>
<h4 id="happy-coding">Happy Coding 💚</h4>
]]></content:encoded></item><item><title><![CDATA[2 Important! Array methods you should know.]]></title><description><![CDATA[Hey everyone 👋
Today's article is about two interesting (also important) array methods:  

.flat()   
.flatMap()  

Let's get started 🚀
1. .flat():
.flat() method recusively flattens the elements which are array into the original array and returns ...]]></description><link>https://blog.sidmirza.com/2-important-array-methods-you-should-know</link><guid isPermaLink="true">https://blog.sidmirza.com/2-important-array-methods-you-should-know</guid><category><![CDATA[Web Development]]></category><category><![CDATA[JavaScript]]></category><category><![CDATA[#codenewbies]]></category><category><![CDATA[array]]></category><dc:creator><![CDATA[Mohd Shahid]]></dc:creator><pubDate>Sun, 22 Aug 2021 07:50:34 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1629618111086/V374ZLheo.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h1 id="hey-everyone">Hey everyone 👋</h1>
<p>Today's article is about two interesting (<em>also important</em>) array methods:  </p>
<ol>
<li><code>.flat()</code>   </li>
<li><code>.flatMap()</code>  </li>
</ol>
<h3 id="lets-get-started">Let's get started 🚀</h3>
<h4 id="1-flat">1. <code>.flat()</code>:</h4>
<p><code>.flat()</code> method recusively flattens the elements which are array into the original array and returns a new array.  </p>
<h4 id="examples">Examples 👇</h4>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> array = [<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,[<span class="hljs-number">3</span>,<span class="hljs-number">4</span>]];
<span class="hljs-keyword">const</span> newArray = array.flat();
<span class="hljs-built_in">console</span>.log(newArray);
<span class="hljs-comment">// [1,2,3,4]</span>
</code></pre>
<p>👉 <code>.flat()</code> receives an optional argument <code>depth</code> (1 by default).</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> arr2 = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, [<span class="hljs-number">3</span>, <span class="hljs-number">4</span>, [<span class="hljs-number">5</span>, <span class="hljs-number">6</span>]]];
arr2.flat();
<span class="hljs-comment">// [1, 2, 3, 4, [5, 6]]</span>

<span class="hljs-keyword">const</span> arr3 = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, [<span class="hljs-number">3</span>, <span class="hljs-number">4</span>, [<span class="hljs-number">5</span>, <span class="hljs-number">6</span>]]];
arr3.flat(<span class="hljs-number">2</span>);
<span class="hljs-comment">// [1, 2, 3, 4, 5, 6]</span>
</code></pre>
<p>👉 The <code>.flat()</code> method removes empty slots in arrays:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> arr5 = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, , <span class="hljs-number">4</span>, <span class="hljs-number">5</span>];
arr5.flat();
<span class="hljs-comment">// [1, 2, 4, 5]</span>
</code></pre>
<p></p><hr /><p></p>
<h4 id="2-flatmap">2. <code>.flatMap()</code>:</h4>
<p><code>.flatMap()</code> is identical to a <code>.map()</code> followed by a <code>.flat()</code> of depth 1.  </p>
<h4 id="examples">Examples 👇</h4>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> arr1 = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>];

arr1.map(<span class="hljs-function"><span class="hljs-params">x</span> =&gt;</span> [x * <span class="hljs-number">2</span>]);
<span class="hljs-comment">// [[2], [4], [6], [8]]</span>

arr1.flatMap(<span class="hljs-function"><span class="hljs-params">x</span> =&gt;</span> [x * <span class="hljs-number">2</span>]);
<span class="hljs-comment">// [2, 4, 6, 8]</span>
</code></pre>
<p>👉 <code>.flatMap()</code> only flattens the array up to depth 1.  </p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> arr1 = [<span class="hljs-string">"it's Sunny in"</span>, <span class="hljs-string">""</span>, <span class="hljs-string">"California"</span>];

arr1.map(<span class="hljs-function"><span class="hljs-params">x</span> =&gt;</span> x.split(<span class="hljs-string">" "</span>));
<span class="hljs-comment">// [["it's","Sunny","in"],[""],["California"]]</span>

arr1.flatMap(<span class="hljs-function"><span class="hljs-params">x</span> =&gt;</span> x.split(<span class="hljs-string">" "</span>));
<span class="hljs-comment">// ["it's","Sunny","in", "", "California"]</span>
</code></pre>
<p>👉 As you know <code>.map</code> always works <em>one-to-one</em>, but <code>flatMap</code> can be used to modify the number of items during <code>map</code>.</p>
<p>A nice example I found on MDN 👇</p>
<pre><code class="lang-js"><span class="hljs-comment">// Let's say we want to remove all the negative numbers</span>
<span class="hljs-comment">// and split the odd numbers into an even number and a 1</span>
<span class="hljs-keyword">let</span> a = [<span class="hljs-number">5</span>, <span class="hljs-number">4</span>, <span class="hljs-number">-3</span>, <span class="hljs-number">20</span>, <span class="hljs-number">17</span>, <span class="hljs-number">-33</span>, <span class="hljs-number">-4</span>, <span class="hljs-number">18</span>]
<span class="hljs-comment">//       |\  \  x   |  | \   x   x   |</span>
<span class="hljs-comment">//      [4,1, 4,   20, 16, 1,       18]</span>

a.flatMap( <span class="hljs-function">(<span class="hljs-params">n</span>) =&gt;</span>
  (n &lt; <span class="hljs-number">0</span>) ?      [] :
  (n % <span class="hljs-number">2</span> == <span class="hljs-number">0</span>) ? [n] :
                 [n<span class="hljs-number">-1</span>, <span class="hljs-number">1</span>]
)

<span class="hljs-comment">// [4, 1, 4, 20, 16, 1, 18]</span>
</code></pre>
<p>That's it for this article, I hope you found it useful. 😊<br />Take a look on my article on <a target="_blank" href="https://sidmirza.hashnode.dev/array-methods-in-javascript-when-to-use-which">array methods</a>.<br />Please leave a like and follow me on <a target="_blank" href="https://www.twitter.com/sidmirza4">twitter</a>.  </p>
<h4 id="thanks-for-reading">Thanks for reading. 💚</h4>
<h4 id="happy-coding">Happy coding.</h4>
]]></content:encoded></item><item><title><![CDATA[Array methods in JavaScript: when to use which 🤔?]]></title><description><![CDATA[Hey guys 👋
There are a lot of array methods in JavaScript and often we get confused 😕 about which to use when.In this article I will summarise these methods and do my best to clear about which method should we use according to our needs.   
Let's g...]]></description><link>https://blog.sidmirza.com/array-methods-in-javascript-when-to-use-which</link><guid isPermaLink="true">https://blog.sidmirza.com/array-methods-in-javascript-when-to-use-which</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[Web Development]]></category><category><![CDATA[webdev]]></category><category><![CDATA[array methods]]></category><category><![CDATA[array]]></category><dc:creator><![CDATA[Mohd Shahid]]></dc:creator><pubDate>Sun, 15 Aug 2021 07:13:08 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1629010176438/hEnWBl8oe.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="hey-guys">Hey guys 👋</h2>
<p>There are a lot of array methods in JavaScript and often we get confused 😕 about which to use when.<br />In this article I will summarise these methods and do my best to clear about which method should we use according to our needs.   </p>
<h3 id="lets-get-started">Let's get started 🚀</h3>
<p>As I said we will study the array methods according to our needs, so just think what do you want...</p>
<h3 id="i-want">I want...</h3>
<h3 id="1-to-mutate-the-original-array">1. to mutate the original array</h3>
<h4 id="a-add-to-original-array">a) add to original array</h4>
<p>i) <code>.push</code>:<br />this method adds an element to the <strong><em>end</em></strong> of the original array and returns <strong><em>the new length of the array</em></strong>.</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> numbers = [<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>];
number.push(<span class="hljs-number">4</span>);
<span class="hljs-built_in">console</span>.log(numbers) <span class="hljs-comment">// [1,2,3,4]</span>
</code></pre>
<p>ii) <code>.unshift</code>:<br /> this method is like <code>.push</code> method, except it adds the element at the <strong><em>start</em></strong> of the original array.</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> numbers = [<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>];
numbers.unshift(<span class="hljs-number">1</span>);
<span class="hljs-built_in">console</span>.log(numbers) <span class="hljs-comment">// [1,2,3,4]</span>
</code></pre>
<h4 id="b-remove-from-the-original-array">b) remove from the original array</h4>
<p>i) <code>.pop</code>:<br />this method removes the <strong><em>last</em></strong> element of the array and <strong><em>returns the removed element</em></strong>.  </p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> names = [<span class="hljs-string">'Sid'</span>, <span class="hljs-string">'Marty'</span>, <span class="hljs-string">'John'</span>];
<span class="hljs-keyword">const</span> removedName = names.pop();
<span class="hljs-built_in">console</span>.log(names) <span class="hljs-comment">// ['Sid', 'Marty']</span>
<span class="hljs-built_in">console</span>.log(removedName) <span class="hljs-comment">// 'John'</span>
</code></pre>
<p>ii) <code>.shift</code>:<br /><code>.shift</code> is just like <code>.pop</code> except it removes the element from the <strong><em>start</em></strong>.</p>
<p>iii) <code>.splice</code>:<br />this method is bit tricky, it can <strong><em>remove</em></strong> and/or <strong><em>add</em></strong> the element(s) to the original array.</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> fruits = [<span class="hljs-string">"Banana"</span>, <span class="hljs-string">"Orange"</span>, <span class="hljs-string">"Apple"</span>, <span class="hljs-string">"Mango"</span>];

<span class="hljs-comment">// At position 2, remove 1 element and add 2 elements:</span>
fruits.splice(<span class="hljs-number">2</span>, <span class="hljs-number">1</span>, <span class="hljs-string">"Lemon"</span>, <span class="hljs-string">"Kiwi"</span>);
<span class="hljs-built_in">console</span>.log(fruits) <span class="hljs-comment">// ["Banana", "Orange", "Lemon", "Kiwi", "Mongo"]</span>
</code></pre>
<p>Other mutating array methods:<br />i)  <code>.reverse</code><br />this method reverses an array. The first element becomes the last, and the last array becomes the first.</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> array = [<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>]
array.reverse();
<span class="hljs-built_in">console</span>.log(array);
<span class="hljs-comment">// [5,4,3,2,1]</span>
</code></pre>
<p>ii) <code>.sort</code><br />this method sorts the array <strong>in place</strong> and also <strong>returns the sorted array</strong>. This method converts all the element to string and sorts them in ascending order by default.  </p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> months = [<span class="hljs-string">'March'</span>, <span class="hljs-string">'Jan'</span>, <span class="hljs-string">'Feb'</span>, <span class="hljs-string">'Dec'</span>];
months.sort();
<span class="hljs-built_in">console</span>.log(months);
<span class="hljs-comment">// ["Dec", "Feb", "Jan", "March"]</span>

<span class="hljs-keyword">const</span> array1 = [<span class="hljs-number">1</span>, <span class="hljs-number">30</span>, <span class="hljs-number">4</span>, <span class="hljs-number">21</span>, <span class="hljs-number">100000</span>];
array1.sort();
<span class="hljs-built_in">console</span>.log(array1);
<span class="hljs-comment">// [1, 100000, 21, 30, 4]</span>
</code></pre>
<p><code>.sort</code> receives an optional parameter <strong>a compare function</strong>, which can be used to override the default behaviour of <code>.sort</code>.<br />If <em>compare function</em> is supplied all array elements are sorted according to the return value of the compare function.<br /><strong>compare function</strong> receives two arguments  say <code>a</code> and <code>b</code>.  Where <code>a</code> is <em>first element for comparison</em> and <code>b</code> is <em>second element for comparison.</em>  </p>
<ul>
<li>If compareFunction(a, b) returns a value &gt; than 0, sort b before a.</li>
<li>If compareFunction(a, b) returns a value &lt; than 0, sort a before b.</li>
<li>If compareFunction(a, b) returns 0, a and b are considered equal.</li>
</ul>
<p><em>I know it sounds confusing 😅 , take a look at the example 👇</em>.</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> numbers = [<span class="hljs-number">4</span>, <span class="hljs-number">2</span>, <span class="hljs-number">5</span>, <span class="hljs-number">1</span>, <span class="hljs-number">3</span>];
numbers.sort(<span class="hljs-function">(<span class="hljs-params">a, b</span>) =&gt;</span> {
  <span class="hljs-keyword">return</span> a - b;
})
<span class="hljs-built_in">console</span>.log(numbers);
<span class="hljs-comment">// [1,2,3,4,5]</span>
</code></pre>
<p>iii) <code>.fill</code><br />The <code>.fill</code> method changes all elements in an array to provided value, from a start index (which is 0 by default) to an end index (which is <em>length of the array</em> by default).  </p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> array1 = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>];
<span class="hljs-comment">// fill with 0 from position 2 until position 4</span>
<span class="hljs-built_in">console</span>.log(array1.fill(<span class="hljs-number">0</span>, <span class="hljs-number">2</span>, <span class="hljs-number">4</span>));
<span class="hljs-comment">// [1, 2, 0, 0]</span>


<span class="hljs-comment">// fill with 5 from position 1</span>
<span class="hljs-built_in">console</span>.log(array1.fill(<span class="hljs-number">5</span>, <span class="hljs-number">1</span>));
<span class="hljs-comment">// [1, 5, 5, 5]</span>

<span class="hljs-built_in">console</span>.log(array1.fill(<span class="hljs-number">6</span>));
<span class="hljs-comment">// [6, 6, 6, 6]</span>
</code></pre>
<hr />

<h3 id="2-a-new-array">2. a new array</h3>
<p>If you want a new array, please look at the following array methods:  </p>
<p>i) <code>.map</code>:<br />As a react developer, <code>.map</code> is the most used array method for me. It loops over the array and perform a certain action on each element then returns the <strong><em>new array of the same length</em></strong>.</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> numbers = [<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>];
<span class="hljs-keyword">const</span> numberSqr = numbers.map(<span class="hljs-function">(<span class="hljs-params">num</span>) =&gt;</span> num*num);
<span class="hljs-built_in">console</span>.log(numberSqr) <span class="hljs-comment">// [1,4,9,16]</span>
</code></pre>
<ul>
<li><p><code>.map</code> receives a callback function, which accepts the following arguments:<br />i) The current <code>element</code>  being processed in the array.<br />ii) <code>index</code> of the current <code>element</code> being processed in the array.<br />iii) <code>array</code> on which <code>.map</code> was called.  </p>
</li>
<li><p>value returned from the callback function will be mapped the corresponding element in the array. </p>
</li>
</ul>
<p>ii) <code>.filter</code>:<br />This methods <strong><em>creates a new array</em></strong> with all the elements that passed the condition given in the callback function.  </p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> words = [<span class="hljs-string">'spray'</span>, <span class="hljs-string">'limit'</span>, <span class="hljs-string">'elite'</span>, <span class="hljs-string">'exuberant'</span>, <span class="hljs-string">'destruction'</span>, <span class="hljs-string">'present'</span>];

<span class="hljs-keyword">const</span> result = words.filter(<span class="hljs-function"><span class="hljs-params">word</span> =&gt;</span> word.length &gt; <span class="hljs-number">6</span>);

<span class="hljs-built_in">console</span>.log(result);
<span class="hljs-comment">// ["exuberant", "destruction", "present"]</span>
</code></pre>
<p>iii) <code>.slice</code>:<br />This method returns a copy of the <strong><em>portion</em></strong> of the array.</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> animals = [<span class="hljs-string">'ant'</span>, <span class="hljs-string">'bison'</span>, <span class="hljs-string">'camel'</span>, <span class="hljs-string">'duck'</span>, <span class="hljs-string">'elephant'</span>];

<span class="hljs-built_in">console</span>.log(animals.slice(<span class="hljs-number">2</span>));
<span class="hljs-comment">// ["camel", "duck", "elephant"]</span>

<span class="hljs-built_in">console</span>.log(animals.slice(<span class="hljs-number">2</span>, <span class="hljs-number">4</span>));
<span class="hljs-comment">// ["camel", "duck"]</span>
</code></pre>
<p>iv) <code>.concat</code>:<br />This method is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array.</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> letters = [<span class="hljs-string">'a'</span>, <span class="hljs-string">'b'</span>, <span class="hljs-string">'c'</span>];
<span class="hljs-keyword">const</span> numbers = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>];

letters.concat(numbers);
<span class="hljs-comment">// result is ['a', 'b', 'c', 1, 2, 3]</span>
</code></pre>
<hr />

<h3 id="3-an-array-index">3. an array index</h3>
<p>i) <code>.indexOf</code>:<br />This method <strong><em>returns the first index</em></strong> at which a given element can be found in the array, or <strong><em>-1</em></strong>   if it is not present.  </p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> fruits = [<span class="hljs-string">'Banana'</span>, <span class="hljs-string">'Apple'</span>, <span class="hljs-string">'Kiwi'</span>];
<span class="hljs-built_in">console</span>.log(fruits.indexOf(<span class="hljs-string">'Apple'</span>)) <span class="hljs-comment">// 1</span>
<span class="hljs-built_in">console</span>.log(fruits.indexOf(<span class="hljs-string">'Orange'</span>)) <span class="hljs-comment">// -1</span>
</code></pre>
<p>ii) <code>.findIndex</code>:<br />This method <strong>returns the index of the first element</strong> that passed a given condition. Otherwise <strong>-1</strong> indicating that no element passed the condition.</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> numbers = [<span class="hljs-number">5</span>,<span class="hljs-number">9</span>,<span class="hljs-number">2</span>,<span class="hljs-number">11</span>,<span class="hljs-number">5</span>];
<span class="hljs-keyword">const</span> index = numbers.findIndex(<span class="hljs-function">(<span class="hljs-params">element</span>) =&gt;</span> element &gt; <span class="hljs-number">8</span>) <span class="hljs-comment">// 2</span>
<span class="hljs-keyword">const</span> ind = numbers.findIndex(<span class="hljs-function">(<span class="hljs-params">element</span>) =&gt;</span> element &gt; <span class="hljs-number">12</span>) <span class="hljs-comment">// -1</span>
</code></pre>
<hr />

<h3 id="4-an-array-element">4. an array element</h3>
<p><code>.find</code>:<br />This method returns the first element which satisfies a provided condition. <code>undefined</code> otherwise.</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> array1 = [<span class="hljs-number">5</span>, <span class="hljs-number">12</span>, <span class="hljs-number">8</span>, <span class="hljs-number">130</span>, <span class="hljs-number">44</span>];

<span class="hljs-keyword">const</span> found = array1.find(<span class="hljs-function"><span class="hljs-params">element</span> =&gt;</span> element &gt; <span class="hljs-number">10</span>);

<span class="hljs-built_in">console</span>.log(found);
<span class="hljs-comment">// 12</span>
</code></pre>
<hr />

<h3 id="5-to-know-if-the-array-includes">5. to know if the array includes</h3>
<p>i) <code>.includes</code>:<br />This methods returns <code>true</code> (if the array contains the element) or <code>false</code>.</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> friends = [<span class="hljs-string">'Jon'</span>, <span class="hljs-string">'Joe'</span>, <span class="hljs-string">'Jack'</span>, <span class="hljs-string">'Jill'</span>];
<span class="hljs-built_in">console</span>.log(friends.includes(<span class="hljs-string">'Jon'</span>)) <span class="hljs-comment">// true</span>
<span class="hljs-built_in">console</span>.log(friends.includes(<span class="hljs-string">'Sid'</span>)) <span class="hljs-comment">// false</span>
</code></pre>
<p>ii) <code>.some</code>:<br />Name of this method sometimes confuse me 😣. This method returns <code>true</code> if at least one element passes the given condition.</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> array = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>];

<span class="hljs-comment">// checks whether an element is even</span>
<span class="hljs-keyword">const</span> even = <span class="hljs-function">(<span class="hljs-params">element</span>) =&gt;</span> element % <span class="hljs-number">2</span> === <span class="hljs-number">0</span>;

<span class="hljs-built_in">console</span>.log(array.some(even));
<span class="hljs-comment">// expected output: true</span>
</code></pre>
<p>iii) <code>.every</code>:<br />This method returns <code>true</code> if all the elements in the array pass the given condition, <code>false</code> otherwise.</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">isBigEnough</span>(<span class="hljs-params">element, index, array</span>) </span>{
  <span class="hljs-keyword">return</span> element &gt;= <span class="hljs-number">10</span>;
}
[<span class="hljs-number">12</span>, <span class="hljs-number">5</span>, <span class="hljs-number">8</span>, <span class="hljs-number">130</span>, <span class="hljs-number">44</span>].every(isBigEnough);   <span class="hljs-comment">// false</span>
[<span class="hljs-number">12</span>, <span class="hljs-number">54</span>, <span class="hljs-number">18</span>, <span class="hljs-number">130</span>, <span class="hljs-number">44</span>].every(isBigEnough); <span class="hljs-comment">// true</span>
</code></pre>
<hr />

<h3 id="6-a-new-string">6. a new string</h3>
<p><code>.join</code>:<br />This methods <strong><em>joins</em></strong> all the element of the array by a given string separator and <strong><em>return the string</em></strong>.  </p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> words = [<span class="hljs-string">'JS'</span>, <span class="hljs-string">'is'</span>, <span class="hljs-string">'amazing'</span>];
<span class="hljs-comment">// joining the words by space</span>
<span class="hljs-built_in">console</span>.log(words.join(<span class="hljs-string">' '</span>)) <span class="hljs-comment">// 'JS is amazing'</span>

<span class="hljs-comment">// joining by dash ('-')</span>
<span class="hljs-built_in">console</span>.log(words.join(<span class="hljs-string">'-'</span>)) <span class="hljs-comment">// 'JS-is-amazing'</span>
</code></pre>
<h3 id="7-to-just-loop-over-an-array">7. to just loop over an array</h3>
<p><code>forEach</code>:<br />This method executes a provided function once for each array element.  </p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> array1 = [<span class="hljs-string">'a'</span>, <span class="hljs-string">'b'</span>, <span class="hljs-string">'c'</span>];
array1.forEach(<span class="hljs-function"><span class="hljs-params">element</span> =&gt;</span> <span class="hljs-built_in">console</span>.log(element));
<span class="hljs-comment">// 'a'</span>
<span class="hljs-comment">// 'b'</span>
<span class="hljs-comment">// 'c'</span>
</code></pre>
<h3 id="8-to-transform-the-array-to-a-single-value">8. to transform the array to a single value</h3>
<p><code>.reduce</code>:<br />This methods reduces the array to a <strong>single</strong> <code>value</code>.<br />This <code>value</code> can be of any type: <code>number</code>, <code>string</code>, <code>boolean</code>, <code>array</code> or <code>object</code>.   </p>
<p>The reducer function takes four arguments:<br />a) Accumulator<br />b) Current Value<br />c) Current Index<br />d) Source Array  </p>
<p>Reducer function's returned value is assigned to the accumulator, whose value is remembered across each iteration throughout the array, and ultimately becomes the final, single resulting value.  </p>
<pre><code class="lang-js"><span class="hljs-comment">// sum of the elements of the array using .reduce</span>
<span class="hljs-keyword">let</span> numbers = [<span class="hljs-number">1</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>];
<span class="hljs-keyword">const</span> sum = numbers.reduce(<span class="hljs-function">(<span class="hljs-params">acc, el, i, arr</span>) =&gt;</span> acc+el);
<span class="hljs-built_in">console</span>.log(sum) <span class="hljs-comment">// 10</span>
</code></pre>
<p><br /></p>
<p>Phew, this was a lot to take in  😅.  </p>
<p>I hope you guys found this article helpful, if you did please leave a like.<br />Find me on <a target="_blank" href="https://www.twitter.com/sidmirza4">twitter</a>.  </p>
<h4 id="thanks-for-reading">Thanks for reading. 💚</h4>
<h4 id="happy-coding">Happy coding.</h4>
]]></content:encoded></item><item><title><![CDATA[Logical Assignment Operators in JS]]></title><description><![CDATA[Logical assignment operators are a very new feature in JavaScript.Never heard of them? then this article is for you.    
There are three logical assignmet operators in JavaScript

Logical OR assignment operator (||=)
Logical AND assignment operator (...]]></description><link>https://blog.sidmirza.com/logical-assignment-operators-in-js</link><guid isPermaLink="true">https://blog.sidmirza.com/logical-assignment-operators-in-js</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[javascript modules]]></category><category><![CDATA[Web Development]]></category><category><![CDATA[webdev]]></category><dc:creator><![CDATA[Mohd Shahid]]></dc:creator><pubDate>Fri, 13 Aug 2021 15:18:48 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1628867688927/IGvezjtUQ.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Logical assignment operators are a very new feature in JavaScript.<br />Never heard of them? then this article is for you.    </p>
<p>There are three logical assignmet operators in JavaScript</p>
<ol>
<li>Logical <strong>OR</strong> assignment operator (||=)</li>
<li>Logical <strong>AND</strong> assignment operator (&amp;&amp;=)</li>
<li>Nullish assignment (??=)</li>
</ol>
<h3 id="1-logical-or-assignment">1. Logical <strong>OR</strong> assignment:</h3>
<p>It performs an assignment if the logical operation would evaluate the right-hand side. In other words, <code>x ||= y</code> is equivalent to:</p>
<p><code>x || (x = y)</code>;</p>
<p>which means that the value of y will be assigned to x if x is falsy.</p>
<h4 id="examples">Examples</h4>
<pre><code>let <span class="hljs-type">name</span>;  
<span class="hljs-type">name</span> ||= <span class="hljs-string">'John'</span>  
console.log(<span class="hljs-type">name</span>)  // John  

let person = {<span class="hljs-type">name</span>: <span class="hljs-string">'Percy'</span>};  
person.age ||= <span class="hljs-number">30</span>;
console.log(person) // {<span class="hljs-type">name</span>: <span class="hljs-string">'Percy'</span>, age: <span class="hljs-number">30</span>}
</code></pre><blockquote>
<p>Reference:  <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_OR_assignment">Logical OR Assignment MDN</a> </p>
</blockquote>
<h3 id="2-logical-and-assignment">2. Logical <strong>AND</strong> assignment:</h3>
<p>The logical AND assignment <code>(x &amp;&amp;= y)</code> operator only assigns if x is truthy.  </p>
<pre><code><span class="hljs-string">let</span> <span class="hljs-string">a</span> <span class="hljs-string">=</span> <span class="hljs-number">1</span><span class="hljs-string">;</span>
<span class="hljs-string">let</span> <span class="hljs-string">b</span> <span class="hljs-string">=</span> <span class="hljs-number">0</span><span class="hljs-string">;</span>

<span class="hljs-string">a</span> <span class="hljs-string">&amp;&amp;=</span> <span class="hljs-number">2</span><span class="hljs-string">;</span>
<span class="hljs-string">console.log(a);</span>
<span class="hljs-string">//</span> <span class="hljs-attr">expected output:</span> <span class="hljs-number">2</span>

<span class="hljs-string">b</span> <span class="hljs-string">&amp;&amp;=</span> <span class="hljs-number">2</span><span class="hljs-string">;</span>
<span class="hljs-string">console.log(b);</span>
<span class="hljs-string">//</span> <span class="hljs-attr">expected output:</span> <span class="hljs-number">0</span>
</code></pre><blockquote>
<p>Reference: <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_AND_assignment">Logical AND Operator MDN</a></p>
</blockquote>
<h3 id="3-logical-nullish-assignment">3. Logical Nullish assignment:</h3>
<p>The logical nullish assignment <code>(x ??= y)</code> operator only assigns if x is nullish (null or undefined).  </p>
<pre><code><span class="hljs-string">function</span> <span class="hljs-string">config(options)</span> {
  <span class="hljs-string">options.duration</span> <span class="hljs-string">??=</span> <span class="hljs-number">100</span><span class="hljs-string">;</span>
  <span class="hljs-string">options.speed</span> <span class="hljs-string">??=</span> <span class="hljs-number">25</span><span class="hljs-string">;</span>
  <span class="hljs-string">return</span> <span class="hljs-string">options;</span>
}

<span class="hljs-string">config({</span> <span class="hljs-attr">duration:</span> <span class="hljs-number">125</span> <span class="hljs-string">});</span> <span class="hljs-string">//</span> { <span class="hljs-attr">duration:</span> <span class="hljs-number">125</span>, <span class="hljs-attr">speed:</span> <span class="hljs-number">25</span> }
<span class="hljs-string">config({});</span> <span class="hljs-string">//</span> { <span class="hljs-attr">duration:</span> <span class="hljs-number">100</span>, <span class="hljs-attr">speed:</span> <span class="hljs-number">25</span> }
</code></pre><blockquote>
<p>Reference: <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_nullish_assignment">Logical Nullish assignment MDN</a></p>
</blockquote>
<p>That's it for this blog guys, thank you for reading.<br />I hope you found it helpful, if you did please give a like and share.<br />This is my first blog on hashnode, if there is any mistake or room for improvement, please let me know.</p>
<p><a target="_blank" href="https://twitter.com/sidmirza4">Follow me on twitter</a></p>
]]></content:encoded></item></channel></rss>