<?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: Rakshit</title>
    <description>The latest articles on Forem by Rakshit (@rakshit).</description>
    <link>https://forem.com/rakshit</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%2F376935%2Fa75c41c9-357e-4fc5-a3ca-fcd495ea61b7.jpeg</url>
      <title>Forem: Rakshit</title>
      <link>https://forem.com/rakshit</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/rakshit"/>
    <language>en</language>
    <item>
      <title>When to use React.useCallback Hook</title>
      <dc:creator>Rakshit</dc:creator>
      <pubDate>Mon, 01 Aug 2022 07:51:00 +0000</pubDate>
      <link>https://forem.com/rakshit/when-to-use-reactusecallback-hook-4kji</link>
      <guid>https://forem.com/rakshit/when-to-use-reactusecallback-hook-4kji</guid>
      <description>&lt;p&gt;When I first read about the &lt;code&gt;useCallback&lt;/code&gt; hook I thought I had a great weapon with me to optimise my React app's performance and started using it on every damn function without understanding the limitations or may be I should call it the right concept behind it.&lt;/p&gt;

&lt;p&gt;Before diving deep into this topic, let us first understand on a high level what exactly is the &lt;code&gt;useCallback&lt;/code&gt; hook.&lt;/p&gt;

&lt;p&gt;So, basically &lt;code&gt;useCallback&lt;/code&gt; hook takes a function and a dependency array. It returns the memoized function. A new memoized value of this function is created whenever the value or references of the elements in the dependency array change.&lt;/p&gt;

&lt;h3&gt;
  
  
  What if you do not wrap a function using &lt;code&gt;useCallback&lt;/code&gt;?
&lt;/h3&gt;

&lt;p&gt;When you don't wrap a function with &lt;code&gt;useCallback&lt;/code&gt;, whenever the component is re-rendered a new instance of the function is created(The function is given a new memory location).&lt;/p&gt;

&lt;p&gt;Also, make a note of the below snippet.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;add1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;add2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="nx"&gt;add1&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 3&lt;/span&gt;
&lt;span class="nx"&gt;add2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 3&lt;/span&gt;

&lt;span class="nx"&gt;add1&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;add2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above snippet you can see that though add1 and add2 are created from the same function declaration and give the same output they are not the same because the references of these two functions are different. &lt;/p&gt;

&lt;h3&gt;
  
  
  When to use &lt;code&gt;useCallback&lt;/code&gt;?
&lt;/h3&gt;

&lt;p&gt;Let us consider an example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;Child&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;handler&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handler&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nx"&gt;Click&lt;/span&gt; &lt;span class="nx"&gt;Me&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;memo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Child&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Below is the Parent component&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;ParentComponent&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setState&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;dep&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;handler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useCallback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;You clicked handler&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;dep&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;statehanddler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;setState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;statehanddler&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;State&lt;/span&gt; &lt;span class="nx"&gt;Change&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Child&lt;/span&gt; &lt;span class="nx"&gt;handler&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handler&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example we have wrapped the Child component with &lt;code&gt;React.memo&lt;/code&gt; which means it will re-render the child component only if the props to it change.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;handler&lt;/code&gt; is passed as a prop to the Child component. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Let us assume we did not use &lt;code&gt;useCallback&lt;/code&gt; in the above example.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In this case whenever we click the State Change button the value of &lt;code&gt;state&lt;/code&gt; is changed and the parent component is re-rendered. Since, at every re-render there will be a new instance of every function created we would have a new instance of the handler function.&lt;/p&gt;

&lt;p&gt;Now, what would happen to the child component? Will it re-render?&lt;/p&gt;

&lt;p&gt;In the &lt;code&gt;add&lt;/code&gt; example I have shown you how does function equality works. By referring to it we can say that the child component will re-render because the &lt;code&gt;handler&lt;/code&gt; prop now has a new reference. This means that even when we wrap the component with &lt;code&gt;React.memo&lt;/code&gt; we are a re-rendering the child component.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Assuming we are using &lt;code&gt;useCallback&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;useCallback&lt;/code&gt; hook here will memoize the function passed to it as an argument and it will only create a new instance of the memoized function if the value or reference to an element in the dependency array changes. &lt;/p&gt;

&lt;p&gt;So, clicking on the State Change button will change the value of the state variable &lt;code&gt;state&lt;/code&gt; but the value inside the dependency array(&lt;code&gt;dep&lt;/code&gt;) remains same. Hence, there is no new instance of handler created and the child component will not re-render.&lt;/p&gt;

&lt;h3&gt;
  
  
  When not to use &lt;code&gt;useCallback&lt;/code&gt;?
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;useCallback&lt;/code&gt; has its own downsides. There are times when using &lt;code&gt;useCallback&lt;/code&gt; makes no sense.&lt;/p&gt;

&lt;p&gt;Let us take an example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;Parent&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;clickHandler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useCallback&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Click event&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[])&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Child&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;clickHandler&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Child&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;clickHandler&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;clickHandler&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Child&lt;/span&gt; &lt;span class="nx"&gt;Component&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example using &lt;code&gt;useCallback&lt;/code&gt; makes no sense since we are creating &lt;code&gt;clickHandler&lt;/code&gt; function on every re-render. Also, optimization might cost us more here because of the &lt;code&gt;useCallback&lt;/code&gt; check we have to do on every re-render(Recreation of inline functions is generally cheap).&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;useCallback&lt;/code&gt; memoizes functions instead of values, to prevent recreation upon every render. It helps us avoid unnecessary re-rendering and improves performance.&lt;/p&gt;

&lt;p&gt;We also should be careful while using &lt;code&gt;useCallback&lt;/code&gt; because it can cost us a lot if we do not scan our components well before using it.&lt;/p&gt;

&lt;p&gt;I hope this helps. If you have any questions and suggestions reach me out on &lt;a href="https://github.com/rak-shit"&gt;Github&lt;/a&gt; and &lt;a href="https://www.linkedin.com/in/mrakshit/"&gt;LinkedIn&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Follow me on &lt;a href="https://twitter.com/mRakshit_"&gt;Twitter&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Have a nice day :)&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>hooks</category>
    </item>
    <item>
      <title>Trello Clone with Drag and Drop functionality (No external library used)</title>
      <dc:creator>Rakshit</dc:creator>
      <pubDate>Fri, 22 Jul 2022 19:56:00 +0000</pubDate>
      <link>https://forem.com/rakshit/trello-clone-with-drag-and-drop-functionality-no-external-library-used-3o8l</link>
      <guid>https://forem.com/rakshit/trello-clone-with-drag-and-drop-functionality-no-external-library-used-3o8l</guid>
      <description>&lt;p&gt;Hey guys, I have made a trello clone using React and typescript. There is drag and drop functionality too which does not use any external library.&lt;/p&gt;

&lt;p&gt;Here is the Github link: &lt;a href="https://github.com/rak-shit/trello-clone"&gt;Code&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here is the link to the app: &lt;a href="https://stately-cucurucho-922810.netlify.app/"&gt;Trello Clone&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I would love to write a detailed article on this in the future depending on the reach.&lt;/p&gt;

&lt;p&gt;I hope this helps. If you have any questions and suggestions reach me out on &lt;a href="https://github.com/rak-shit"&gt;Github&lt;/a&gt; and &lt;a href="https://www.linkedin.com/in/mrakshit/"&gt;LinkedIn&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Follow me on &lt;a href="https://twitter.com/mRakshit_"&gt;Twitter&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Have a nice day :)&lt;/p&gt;

</description>
      <category>react</category>
      <category>typescript</category>
      <category>trelloclone</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Functions as First-Class Citizens in Javascript</title>
      <dc:creator>Rakshit</dc:creator>
      <pubDate>Sat, 16 Jul 2022 12:39:12 +0000</pubDate>
      <link>https://forem.com/rakshit/functions-as-first-class-citizens-in-javascript-26mj</link>
      <guid>https://forem.com/rakshit/functions-as-first-class-citizens-in-javascript-26mj</guid>
      <description>&lt;p&gt;Before diving into the topic let me give you some context about functions in Javascript.&lt;/p&gt;

&lt;p&gt;Functions in Javascript are beautiful. They are the heart of Javascript and probably everything in Javascript runs on the chariot called &lt;strong&gt;FUNCTIONS&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Let us first understand what is are Function Statements and Function Expressions.&lt;/p&gt;

&lt;h3&gt;
  
  
  Function Statements
&lt;/h3&gt;

&lt;p&gt;Function Statements are when you create a function and you give it a name. It simply means declaring a function with the &lt;code&gt;function&lt;/code&gt; keyword and giving it a name.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// logic goes here&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above code snippet is nothing but a &lt;strong&gt;Function Statement&lt;/strong&gt;. It is also important to remember that Function Statements are also &lt;strong&gt;Function Declarations&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;So, whenever someone asks you what is a Function Statement or a Function Declaration it is the same.&lt;/p&gt;

&lt;h3&gt;
  
  
  Function Expressions
&lt;/h3&gt;

&lt;p&gt;When functions are assigned to a variable they become Function Expressions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// logic goes here&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above code snippet is an example of a &lt;strong&gt;Function Expression&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The function we used here in the above example is an anonymous function. &lt;/p&gt;

&lt;p&gt;It is important to notice that anonymous functions look like function statements but they are not.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// logic goes here&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above code snipped when complied would throw a &lt;code&gt;SyntaxError&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So, what exactly are anonymous functions?&lt;/p&gt;

&lt;p&gt;Anonymous functions are generally used as values. In the above example we have used an anonymous function as a value to assign it to variable &lt;code&gt;a&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;We also use anonymous functions as a callback function (inside setTimeout). &lt;/p&gt;

&lt;p&gt;There are also another kind of function expressions which are named function expressions. Instead of using anonymous functions we give the function a name.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// logic goes here&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above code snippet is an example of named function expression.&lt;/p&gt;

&lt;p&gt;Now, finally coming to the topic. Functions in Javascript supports all operational properties inherent to other entities. They can be assigned to a variable, passed as an argument to another function (setTimeout), they can also be returned from another function. Basically functions can do whatever every other entity on Javascript does. Hence, the name &lt;strong&gt;First-Class Citizens&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;I hope this article helps. If you have any questions reach me out on &lt;a href="https://github.com/rak-shit"&gt;Github&lt;/a&gt; and &lt;a href="https://www.linkedin.com/in/mrakshit/"&gt;LinkedIn&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Follow me on &lt;a href="https://twitter.com/mRakshit_"&gt;Twitter&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Also, please do check out &lt;a href="https://boot.dev/"&gt;Boot.dev&lt;/a&gt;. Boot.dev is a computer science program. You learn the same kinds of things that you would learn in a 4-year degree from college.&lt;/p&gt;

&lt;p&gt;Have a nice day :)&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>react</category>
      <category>typescript</category>
    </item>
    <item>
      <title>What is Scope in JavaScript?</title>
      <dc:creator>Rakshit</dc:creator>
      <pubDate>Sat, 09 Jul 2022 14:01:03 +0000</pubDate>
      <link>https://forem.com/rakshit/what-is-scope-in-javascript-b3</link>
      <guid>https://forem.com/rakshit/what-is-scope-in-javascript-b3</guid>
      <description>&lt;p&gt;Scope in Javascript refers to the accessibility/visibility of a variable or any other resource in Javascript.&lt;/p&gt;

&lt;p&gt;Without wasting time let us dive into some examples to understand scope.&lt;/p&gt;

&lt;h2&gt;
  
  
  Global Scope
&lt;/h2&gt;

&lt;p&gt;There is only one global scope in Javascript document. The area outside of all the functions is considered t be global scope. The variables declared in global scope can we accessed and altered in any other scope.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//global scope&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;fruit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;apple&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;printApple&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fruit&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;printApple&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example the variable &lt;code&gt;fruit&lt;/code&gt; is &lt;strong&gt;declared&lt;/strong&gt; in global scope and it can accessed any where inside the document. The answer does not change even if I use &lt;code&gt;let&lt;/code&gt; instead of &lt;code&gt;var&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Local Scope
&lt;/h2&gt;

&lt;p&gt;The variables declared inside a function are said to be local to the function they are declared in. Every function has its own scope.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// local scope a&lt;/span&gt;
    &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// local scope b&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// local scope c&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, let us dive a bit into the local scope. Local scope is divided into two sections namely, &lt;strong&gt;block scope&lt;/strong&gt; and &lt;strong&gt;function scope&lt;/strong&gt;. &lt;/p&gt;

&lt;h2&gt;
  
  
  Function Scope
&lt;/h2&gt;

&lt;p&gt;Whenever you declare a variable inside a function the variable is visible or accessible inside the function. The variables declared with the keyword &lt;code&gt;var&lt;/code&gt; are associated with functional scope.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;fruit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;apple&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fruit&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;                &lt;span class="c1"&gt;// apple&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fruit&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// error: fruit is not defined&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Block Scope
&lt;/h2&gt;

&lt;p&gt;In Javascript, the area within &lt;code&gt;if&lt;/code&gt;, &lt;code&gt;for&lt;/code&gt;, &lt;code&gt;switch&lt;/code&gt; and &lt;code&gt;while&lt;/code&gt; is considered to be a block. In simple terms whenever we use &lt;code&gt;{}&lt;/code&gt; (curly braces) a block is created and the variables declared inside it are considered to be block scoped to that particular block.&lt;/p&gt;

&lt;p&gt;The variables declared with the &lt;code&gt;var&lt;/code&gt; keyword do not show the properties of block scope. The variables declared with &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; show the block scope properties.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hello&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
        &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hey&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;z&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;bey&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;z&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="c1"&gt;// console.log(x) will print hello&lt;/span&gt;
&lt;span class="c1"&gt;// console.log(y) will throw error&lt;/span&gt;
&lt;span class="c1"&gt;// console.log(z) will throw error&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Apart from all the scopes discussed above there is one more scope which needs a mention which is the &lt;strong&gt;Lexical Scope&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Lexical Scope
&lt;/h2&gt;

&lt;p&gt;In simple terms it means that the variables declared in the parent can be accessed by the children. The children functions are lexically bound to the execution context of their parents.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hello&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hey&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;z&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;bey&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

    &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;z&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="c1"&gt;// console.log(x) will print hello&lt;/span&gt;
&lt;span class="c1"&gt;// console.log(y) will print hey&lt;/span&gt;
&lt;span class="c1"&gt;// console.log(z) will throw print&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I hope this article sums up what exactly is scope in Javascript. Also, I would like to mention we need to know how exactly &lt;code&gt;let&lt;/code&gt;, &lt;code&gt;const&lt;/code&gt; and &lt;code&gt;var&lt;/code&gt; work in-order to take a much deeper dive into Scope.&lt;/p&gt;

&lt;p&gt;Reach me out on &lt;a href="https://github.com/rak-shit"&gt;Github&lt;/a&gt; and &lt;a href="https://www.linkedin.com/in/mrakshit/"&gt;LinkedIn&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Follow me on &lt;a href="https://twitter.com/mRakshit_"&gt;Twitter&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Have a nice day :)&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>webdev</category>
    </item>
    <item>
      <title>How to iterate over an object in Javascript?</title>
      <dc:creator>Rakshit</dc:creator>
      <pubDate>Sat, 02 Jul 2022 21:49:56 +0000</pubDate>
      <link>https://forem.com/rakshit/how-to-iterate-over-an-object-in-javascript-2apj</link>
      <guid>https://forem.com/rakshit/how-to-iterate-over-an-object-in-javascript-2apj</guid>
      <description>&lt;p&gt;It was during the initial days when I was learning Javascript, I always used to be struck at a point when Objects are involved which is iterating over them. It also used to be so confused whenever I encountered a problem where I had to create a &lt;code&gt;hashmap&lt;/code&gt; using an empty Object.&lt;/p&gt;

&lt;p&gt;This article will give you a clear picture about how you can and how you cannot iterate over Objects in Javascript.&lt;/p&gt;

&lt;p&gt;You cannot iterate over Javascript objects using the regular methods like &lt;code&gt;map()&lt;/code&gt;, &lt;code&gt;forEach()&lt;/code&gt; or &lt;code&gt;for..of&lt;/code&gt; loop.&lt;/p&gt;

&lt;p&gt;Let there be an object:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;object&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Rakshit&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;23&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;gender&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Male&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using &lt;code&gt;map()&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You will get an error saying &lt;code&gt;TypeError: object.map is not a function&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Using &lt;code&gt;forEach()&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You will again arrive at an error saying &lt;code&gt;TypeError: object.forEach is not a function&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Using &lt;code&gt;for..of&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;objects&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will give you &lt;code&gt;TypeError: object not iterable&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;So, how exactly can you iterate an object in Javascript?&lt;/p&gt;

&lt;p&gt;Here are some of the ways you can do that: &lt;/p&gt;

&lt;p&gt;One of the most simplest way is to use &lt;code&gt;for..in&lt;/code&gt; loop&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;object&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But, personally I love to iterate over an object using the &lt;code&gt;Object.entries()&lt;/code&gt; method. This method generates an array of all the enumerable properties of the object passed into it as an argument.&lt;/p&gt;

&lt;p&gt;Since, it returns an array you can use any of the methods you use to iterate over an array.&lt;/p&gt;

&lt;p&gt;Using &lt;code&gt;map()&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;entries&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;object&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using &lt;code&gt;forEach()&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;entries&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;object&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using &lt;code&gt;for..of&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;entries&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;object&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's it! Eureka! With this article I am sure you will never forget how to iterate over Objects in Javascript.&lt;/p&gt;

&lt;p&gt;I hope the article helps.&lt;/p&gt;

&lt;p&gt;Reach me out on &lt;a href="https://github.com/rak-shit"&gt;Github&lt;/a&gt; and &lt;a href="https://www.linkedin.com/in/mrakshit/"&gt;LinkedIn&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Follow me on &lt;a href="https://twitter.com/mRakshit_"&gt;Twitter&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Have a nice day :)&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>typescript</category>
      <category>react</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Why you should be careful using setInterval?</title>
      <dc:creator>Rakshit</dc:creator>
      <pubDate>Thu, 30 Jun 2022 20:47:01 +0000</pubDate>
      <link>https://forem.com/rakshit/why-you-should-be-careful-using-setinterval-2ief</link>
      <guid>https://forem.com/rakshit/why-you-should-be-careful-using-setinterval-2ief</guid>
      <description>&lt;p&gt;If your are here, I am sure that you came across the 2 beautiful Web API's &lt;code&gt;setInterval&lt;/code&gt; and &lt;code&gt;setTimeout&lt;/code&gt; and want to make an even more deeper dive into understanding them and how they work.&lt;/p&gt;

&lt;p&gt;Let us jump into the topic of the post...&lt;/p&gt;

&lt;p&gt;I assume that you understand the basic functionalities of &lt;code&gt;setInterval&lt;/code&gt; and &lt;code&gt;setTimeout&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now, before understanding why &lt;code&gt;setInterval&lt;/code&gt; is this mischievous guy, we must first remind ourselves that Javascript is a single threaded language i.e. it can perform no more than one task at a time.&lt;/p&gt;

&lt;p&gt;Let us take an example to explain the problem with &lt;code&gt;setInterval&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;serverCall&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;x&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;toLocaleTimeString&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;3000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;callServerCall&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;setInterval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;run&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;y&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;toLocaleTimeString&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
        &lt;span class="nx"&gt;serverCall&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;callServerCall&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;


&lt;span class="c1"&gt;// Output will be&lt;/span&gt;
&lt;span class="c1"&gt;// y 02:00:54&lt;/span&gt;
&lt;span class="c1"&gt;// y 02:00:55&lt;/span&gt;
&lt;span class="c1"&gt;// y 02:00:56&lt;/span&gt;
&lt;span class="c1"&gt;// y 02:00:57&lt;/span&gt;
&lt;span class="c1"&gt;// x 02:00:57&lt;/span&gt;
&lt;span class="c1"&gt;// y 02:00:58&lt;/span&gt;
&lt;span class="c1"&gt;// x 02:00:58&lt;/span&gt;
&lt;span class="c1"&gt;// y 02:00:59&lt;/span&gt;
&lt;span class="c1"&gt;// x 02:00:59&lt;/span&gt;
&lt;span class="c1"&gt;// y 02:01:00&lt;/span&gt;
&lt;span class="c1"&gt;// x 02:01:00&lt;/span&gt;
&lt;span class="c1"&gt;// y 02:01:01&lt;/span&gt;
&lt;span class="c1"&gt;// ..&lt;/span&gt;
&lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can clearly see that &lt;code&gt;setInterval&lt;/code&gt; keeps on running after every 1 second even without caring if the previously called &lt;code&gt;serverCall&lt;/code&gt; function was executed or not.&lt;/p&gt;

&lt;p&gt;This will prove to be a huge overhead on us because there will be a lot of requests to the server which are queued.&lt;/p&gt;

&lt;h2&gt;
  
  
  Points to be noted
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;When we call asynchronous operations inside &lt;code&gt;setInterval&lt;/code&gt; it will prove to be very costly for us. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Even if your async operation takes lesser time than the &lt;code&gt;delay&lt;/code&gt; set to the &lt;code&gt;setInterval&lt;/code&gt;, the gap between two async calls wouldn't exactly be the &lt;code&gt;delay&lt;/code&gt; set. It would be much lesser than that because the time taken to execute the async operation will also be calculated in the delay.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I hope the article helps.&lt;/p&gt;

&lt;p&gt;Reach me out on &lt;a href="https://github.com/rak-shit"&gt;Github&lt;/a&gt; and &lt;a href="https://www.linkedin.com/in/mrakshit/"&gt;LinkedIn&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Follow me on &lt;a href="https://twitter.com/mRakshit_"&gt;Twitter&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Have a nice day :)&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>typescript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>MERN stack TODO application</title>
      <dc:creator>Rakshit</dc:creator>
      <pubDate>Fri, 15 May 2020 14:30:56 +0000</pubDate>
      <link>https://forem.com/rakshit/mern-stack-todo-application-pa7</link>
      <guid>https://forem.com/rakshit/mern-stack-todo-application-pa7</guid>
      <description>&lt;p&gt;Hello reader, I am sure you might be one of those people who are trying to figure out, How do we connect the React client with the backend?! I assure you by the end of this post you will be able to grasp the basic stuff you need to know in-order to build your own &lt;strong&gt;FullStack&lt;/strong&gt; react web application.&lt;/p&gt;

&lt;p&gt;In this post, I wouldn't be focusing much on the design aspect. Much emphasis will be given to the concept.&lt;/p&gt;

&lt;p&gt;I am assuming that you are quite familiar with the structure of a react client repo. Firstly, you must have the react boilerplate code which can be created using the &lt;strong&gt;create-react-app&lt;/strong&gt; package by &lt;strong&gt;npm&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;create-react-app client
cd client/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Inside the client repo, create another folder called &lt;strong&gt;server&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;mkdir server
cd server/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Initiate the &lt;strong&gt;package.json&lt;/strong&gt; using the below code segment.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm init -y
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;With the &lt;strong&gt;package.json&lt;/strong&gt; file available in the server directory, we are ready to add some dependencies which will be useful in setting up the backend.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install express body-parser cors mongoose
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Let us have an idea about what each of the above package does:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Express:&lt;/strong&gt; Express is a light weighted web framework of Node.js. This acts as our main server and has many compatible middleware to perform almost any kind of function in web development.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;body-parser:&lt;/strong&gt; It is a middleware used to parse the posted data from the client side.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;cors:&lt;/strong&gt; Cross-origin resource sharing (CORS) is a mechanism that allows restricted resources on a web page to be requested from another domain outside the domain from which the first resource was served. And cors is the Node.js package which performs the above function for us.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;mongoose:&lt;/strong&gt; mongoose is an object modelling tool for MongoDB. It helps us access MongoDB in an object oriented way.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Then, we must also install a package called &lt;strong&gt;nodemon&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;npm install -g nodemon
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Nodemon is a utility that will monitor for any changes in your source and automatically restart your server. Nodemon is used to run our Node.js server.&lt;/p&gt;

&lt;p&gt;After this, we need to install MongoDB. In MacOS, we van install and run Mongo by the following commands.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;brew install mongodb
mkdir -p /db/data
mongod
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;After the mongo shell opens:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;use todos
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The above command will create a database named todos.&lt;/p&gt;

&lt;p&gt;If you face any problem with the Mongo installation, you can refer to the official website. It is quite clear there about the installation process of mongodb.&lt;/p&gt;

&lt;p&gt;With this we have installed all the packages we need for the server side.&lt;/p&gt;
&lt;h1&gt;
  
  
  Configuring the server and connecting the database
&lt;/h1&gt;

&lt;p&gt;Let us first create a file called &lt;strong&gt;server.js&lt;/strong&gt; inside the server folder.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;touch server.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Also create a directory called &lt;strong&gt;db&lt;/strong&gt; inside the server and create a file called &lt;strong&gt;index.js&lt;/strong&gt; inside it. This file will handle the connection of the database to the server.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir db
cd db/
touch index.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;The index.js file inside the db directory is given below.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;With the above code, we are creating an express server and we are binding the cors and body-parser middleware to the app object. &lt;/p&gt;

&lt;p&gt;We make use of the mongoose library inside the index.js file of the db directory to connect the database. We are loading the database in the into the server.js file to complete the connection of MongoDB with the express server.&lt;/p&gt;

&lt;p&gt;Also, note that the server listens to the port 3000.&lt;/p&gt;

&lt;p&gt;To run the server:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;nodemon server.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h1&gt;
  
  
  Creating a mongoose schema
&lt;/h1&gt;

&lt;p&gt;Create a &lt;strong&gt;models&lt;/strong&gt; folder inside the server. Inside the models directory, create a file named &lt;strong&gt;todo-model.js&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Mongoose allows us to create a model in an object oriented way.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;With the above code, we are ready to access the Mongo database using the Todo Schema.&lt;/p&gt;

&lt;h1&gt;
  
  
  Endpoint implementation
&lt;/h1&gt;

&lt;p&gt;Create two folders inside the server directory, namely, &lt;strong&gt;routes&lt;/strong&gt; and &lt;strong&gt;controllers&lt;/strong&gt;. Inside the routes folder create a file named &lt;strong&gt;todo-router.js&lt;/strong&gt; and inside the controller folder create a file named &lt;strong&gt;todo-ctrl.js&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;We must first create a router instance inside the todo-router.js. The created router instance will be used to handle the implementations such as adding a Todo item or deleting a Todo item, etc.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const express = require('express')
const router = express.Router()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The above lines will create a router instance.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;todo-ctrl.js will have the implementations of the actions like adding a Todo item or deleting a Todo item.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The todo-ctrl.js file has the code for main implementation of adding a todo item and returning all the todo items.&lt;/p&gt;

&lt;p&gt;Inside the todo-router.js, we define the method of handling the request using the statement&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;router.post('/', todoCtrl.createItem)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Here, &lt;strong&gt;todoCtrl&lt;/strong&gt; is the imported version of the todo-ctrl.js inside the todo-router.js and the createItem is the function which is declared and defined inside the todo-ctrl.js which handles the addition of todo item into the database.&lt;/p&gt;

&lt;p&gt;We can also see the another route which is used to get all the todo items which we need to display on the browser.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;We also must &lt;strong&gt;load the routes&lt;/strong&gt; into the server.js file.&lt;/p&gt;

&lt;p&gt;We can test the API's using Postman. Check the images below&lt;/p&gt;

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

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

&lt;p&gt;With this, we finish the backend implementation of the Todo application.&lt;/p&gt;

&lt;h1&gt;
  
  
  Client side
&lt;/h1&gt;

&lt;p&gt;We have already created the client side boilerplate code. We then create a &lt;strong&gt;components&lt;/strong&gt; folder inside the &lt;strong&gt;src&lt;/strong&gt; folder. We create a file named &lt;strong&gt;Todo.js&lt;/strong&gt; inside the components folder. &lt;/p&gt;

&lt;p&gt;We use &lt;strong&gt;axios&lt;/strong&gt; package of npm for fetching the data from the endpoints.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install axios
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;The clickHandler method is used to post the data to the database. The componentDidMount method is a lifecycle method which gets triggered whenever a component inside the window is altered. So, when the todo item is posted, the endpoint to get all the todos will be hit by the componentDidMount method.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NOTE:&lt;/strong&gt; It is advisable to use the map function in-order to loop around the todo items. For understanding purpose I have used the for loop.&lt;/p&gt;

&lt;p&gt;Make sure that you import the Todo.js inside the App.js and run:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;Open your browser and go to &lt;a href="http://localhost:3001"&gt;http://localhost:3001&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;As you add the items, the items will be updated. &lt;/p&gt;

&lt;p&gt;I also want you to try deleting the todo items from the list. You can also try creating using a good design template.&lt;/p&gt;

&lt;p&gt;The repo for the article can be found &lt;a href="https://github.com/rak-shit/MERN-stack-Todo"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Reach me out on &lt;a href="https://github.com/rak-shit"&gt;Github&lt;/a&gt; and &lt;a href="https://www.linkedin.com/in/mrakshit/"&gt;LinkedIn&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Enjoy :)&lt;/p&gt;

</description>
      <category>react</category>
      <category>mongodb</category>
      <category>node</category>
      <category>express</category>
    </item>
  </channel>
</rss>
