<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Raj Maharjan</title>
    <description>The latest articles on Forem by Raj Maharjan (@rajmaharjan).</description>
    <link>https://forem.com/rajmaharjan</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%2F379875%2Fa7bbaccc-b5c3-48aa-80b8-ca173c06f087.jpeg</url>
      <title>Forem: Raj Maharjan</title>
      <link>https://forem.com/rajmaharjan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/rajmaharjan"/>
    <language>en</language>
    <item>
      <title>Delete merged branches using one command</title>
      <dc:creator>Raj Maharjan</dc:creator>
      <pubDate>Fri, 30 Apr 2021 04:52:42 +0000</pubDate>
      <link>https://forem.com/rajmaharjan/delete-merged-branches-using-one-command-557k</link>
      <guid>https://forem.com/rajmaharjan/delete-merged-branches-using-one-command-557k</guid>
      <description>&lt;p&gt;Here is a command that you can use to delete all the branches that have been merged to &lt;code&gt;master&lt;/code&gt; branch from both local and remote using just one command. Instead of deleting branch one by one, I use below commands to delete all the branches that have been merged.&lt;/p&gt;

&lt;h5&gt;
  
  
  Delete local merged branches:
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git branch --merged master | egrep -v "(^\*|master|dev)" | xargs -n 1 git branch -d
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  Delete remote merged branches:
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git branch -r --merged master | egrep -v "(^\*|master|dev)" | cut -d/ -f2- | xargs -n 1 git push --delete origin
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can change branch name from master to any other branch name that you refer to as your main branch.&lt;/p&gt;

&lt;h5&gt;
  
  
  Also you can use it as alias by adding it in .bashrc file.
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;alias gclb='git branch --merged master | egrep -v "(^\*|master|dev)" | xargs -n 1 git branch -d'
alias gcrb='git branch -r --merged master | egrep -v "(^\*|master|dev)" | cut -d/ -f2- | xargs -n 1 git push --delete origin'

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

&lt;/div&gt;



&lt;p&gt;It has been a lot easier for me to use it this way. If you do it differently or have better way please comment below.&lt;/p&gt;

</description>
      <category>git</category>
      <category>github</category>
      <category>branches</category>
      <category>command</category>
    </item>
    <item>
      <title>DOM Event delegation pattern</title>
      <dc:creator>Raj Maharjan</dc:creator>
      <pubDate>Wed, 15 Jul 2020 12:40:13 +0000</pubDate>
      <link>https://forem.com/rajmaharjan/dom-event-delegation-pattern-kje</link>
      <guid>https://forem.com/rajmaharjan/dom-event-delegation-pattern-kje</guid>
      <description>&lt;p&gt;If you have multiple &lt;code&gt;button&lt;/code&gt; element with a similar callback function inside a &lt;code&gt;div&lt;/code&gt; element, you can delegate event listeners to the parent &lt;code&gt;div&lt;/code&gt; element instead of listening to each button.&lt;/p&gt;

&lt;p&gt;// poor practice&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div&amp;gt;
  &amp;lt;button onclick="myFunction("red")"&amp;gt;Red&amp;lt;/button&amp;gt;
  &amp;lt;button onclick="myFunction("blue")"&amp;gt;Blue&amp;lt;/button&amp;gt;
  &amp;lt;button onclick="myFunction("black")"&amp;gt;Black&amp;lt;/button&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;// good practice&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div onclick="myFunction(event)"&amp;gt;
  &amp;lt;button id="red"&amp;gt;Red&amp;lt;/button&amp;gt;
  &amp;lt;button id="blue"&amp;gt;Blue&amp;lt;/button&amp;gt;
  &amp;lt;button id="black"&amp;gt;Black&amp;lt;/button&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;// script&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function myFunction(event) {
  let target = event.target;
  if (target.nodeName.toLowerCase() !== "button") {
    return;
  }

  // do something with id
  ...
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The drawback is that you have to write a bit more code to filter out unnecessary events, but it will drastically increase the performance and provide cleaner code which outweighs the drawback significantly.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>html</category>
      <category>dom</category>
    </item>
    <item>
      <title>JavaScript Function declaration vs expression</title>
      <dc:creator>Raj Maharjan</dc:creator>
      <pubDate>Sun, 05 Jul 2020 05:13:52 +0000</pubDate>
      <link>https://forem.com/rajmaharjan/javascript-function-declaration-vs-expression-31kj</link>
      <guid>https://forem.com/rajmaharjan/javascript-function-declaration-vs-expression-31kj</guid>
      <description>&lt;h3&gt;
  
  
  Function declaration:
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ZrSeYnKz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/u9zomqzuyhox3nr65jch.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ZrSeYnKz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/u9zomqzuyhox3nr65jch.png" alt="Function expression"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;appear in the global scope&lt;/li&gt;
&lt;li&gt;appear inside a function&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Features:&lt;/strong&gt; Hoisting&lt;br&gt;
&lt;strong&gt;Available:&lt;/strong&gt; Anywhere inside scope (global and local)&lt;/p&gt;

&lt;h3&gt;
  
  
  Function expression:
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--gWcECiUe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ad3k8bdt3ilc1lnlrv77.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--gWcECiUe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ad3k8bdt3ilc1lnlrv77.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;can be assigned to a variable&lt;/li&gt;
&lt;li&gt;can be assigned to a property&lt;/li&gt;
&lt;li&gt;can appear in function invocations as parameters&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Features:&lt;/strong&gt; Closures, Callbacks, IIFE (Immediately Invoked Function Expressions)&lt;br&gt;
&lt;strong&gt;Available:&lt;/strong&gt; after the line it is declared&lt;/p&gt;

</description>
      <category>javascript</category>
    </item>
    <item>
      <title>Convert string to number</title>
      <dc:creator>Raj Maharjan</dc:creator>
      <pubDate>Wed, 24 Jun 2020 02:43:37 +0000</pubDate>
      <link>https://forem.com/rajmaharjan/convert-string-to-number-46ib</link>
      <guid>https://forem.com/rajmaharjan/convert-string-to-number-46ib</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--PBTpYtJf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/4u8n3lv86p3oq9qa9r19.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--PBTpYtJf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/4u8n3lv86p3oq9qa9r19.png" alt="parseInt"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Because:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;em&gt;parseInt(n)&lt;/em&gt; as name suggests, parses and doesn't simply convert.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Caveats:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;For later two, the string should only represent a number otherwise it will return NaN.&lt;/li&gt;
&lt;li&gt;However, &lt;em&gt;parseInt("10 apples")&lt;/em&gt; can still parse string that starts with a number.&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>javascript</category>
      <category>microoptimization</category>
    </item>
    <item>
      <title>Loop micro-optimization</title>
      <dc:creator>Raj Maharjan</dc:creator>
      <pubDate>Thu, 18 Jun 2020 03:55:47 +0000</pubDate>
      <link>https://forem.com/rajmaharjan/loop-micro-optimization-27hn</link>
      <guid>https://forem.com/rajmaharjan/loop-micro-optimization-27hn</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--R-8G6SCx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/nf3izx9rs3xo0kyo6twk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--R-8G6SCx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/nf3izx9rs3xo0kyo6twk.png" alt="loop"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Because:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;For loop uses one less variable&lt;/li&gt;
&lt;li&gt;Count down to 0 is usually faster because it's more efficient to compare to 0 than others.&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>javascript</category>
      <category>microoptimization</category>
    </item>
    <item>
      <title>Difference between i++ and ++i</title>
      <dc:creator>Raj Maharjan</dc:creator>
      <pubDate>Tue, 12 May 2020 01:42:02 +0000</pubDate>
      <link>https://forem.com/rajmaharjan/difference-between-i-and-i-b31</link>
      <guid>https://forem.com/rajmaharjan/difference-between-i-and-i-b31</guid>
      <description>&lt;p&gt;Both prefix and postfix operators are used to mutate and update values. And both &lt;code&gt;i++&lt;/code&gt; and &lt;code&gt;++i&lt;/code&gt; alone does the same thing, increment the value of i by 1. This can sometimes be confusing for some beginners.&lt;/p&gt;

&lt;p&gt;The difference can be found when assigned to other variables. 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;let i = 0;
let j = i++; // first assigns to j then increments i by 1
console.log(j); // 0
let k = ++i; // first increments i by 1 then assigns to k
console.log(k); // 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>javascript</category>
      <category>operator</category>
    </item>
  </channel>
</rss>
