<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: shahabbukhari</title>
    <description>The latest articles on DEV Community by shahabbukhari (@shahabbukhari).</description>
    <link>https://dev.to/shahabbukhari</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%2F319016%2F48da6b1a-ecca-4326-a4c5-0b5228269523.png</url>
      <title>DEV Community: shahabbukhari</title>
      <link>https://dev.to/shahabbukhari</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shahabbukhari"/>
    <language>en</language>
    <item>
      <title>React Custom Hooks Simplified</title>
      <dc:creator>shahabbukhari</dc:creator>
      <pubDate>Fri, 22 Apr 2022 23:57:03 +0000</pubDate>
      <link>https://dev.to/shahabbukhari/react-custom-hooks-simplified-3ljm</link>
      <guid>https://dev.to/shahabbukhari/react-custom-hooks-simplified-3ljm</guid>
      <description>&lt;p&gt;When you build any application Hooks are the most commonly utilized thing,  Hooks like useState, useEffect, or useRef are the most common ones but sometimes they are not enough you need to write your own custom hooks&lt;/p&gt;

&lt;h2&gt;
  
  
  A Bit of History:
&lt;/h2&gt;

&lt;p&gt;Not a long before everything in react was classes and It has been approximately one year since React v16.8 was released, marking the introduction of Hooks. They let you use state and other React features without writing a class.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--stV9hfkS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://y.yarn.co/ca6ccdca-b805-477f-838e-529abd024b29_text.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--stV9hfkS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://y.yarn.co/ca6ccdca-b805-477f-838e-529abd024b29_text.gif" alt="https://y.yarn.co/ca6ccdca-b805-477f-838e-529abd024b29_text.gif" width="400" height="180"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hooks make React so much better because you have simpler code that implements similar functionalities faster and more effectively. You can also implement React state and lifecycle methods without writing classes.&lt;/p&gt;

&lt;p&gt;Let’s see the comparison of class-based components vs functional components.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Class Based&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./styles.css&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt; &lt;span class="kd"&gt;extends&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;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;count&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nx"&gt;clickHandler&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="k"&gt;this&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="nx"&gt;prevState&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="na"&gt;count&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;prevState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
    &lt;span class="p"&gt;}));&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;

  &lt;span class="nx"&gt;render&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="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;clickHandler&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Click To Count&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="k"&gt;this&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;count&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&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="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;App&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// functional components&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&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;Example&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Declare a new state variable, which we'll call "count"&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;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCount&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="mi"&gt;0&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="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;You clicked &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; times&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&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="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        Click me
      &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&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;After React v16.8 one thing was clear now we won’t have to write more classes functions are the future.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--t9etsT_Q--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4ub1rkboh8rkg0hgydui.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--t9etsT_Q--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4ub1rkboh8rkg0hgydui.png" alt="Classes Vs Functions" width="880" height="880"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Custom Hooks:
&lt;/h2&gt;

&lt;p&gt;In simple words, the custom hook is just a functional running inside a component. they use other hooks based on the type of functionality they are providing. custom hooks are so powerful and help you minimize the complexity by creating a level of abstraction and making your code much &lt;code&gt;cleaner&lt;/code&gt; and &lt;code&gt;reusable&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In this blog/tutorial we will be creating a progress bar with the custom hook of &lt;code&gt;useCounter.&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: using &lt;code&gt;**use**&lt;/code&gt; before a hook is a convention. after all it's just a function &lt;strong&gt;Its name should always start with &lt;code&gt;use&lt;/code&gt;&lt;/strong&gt; so that you can tell at a glance that the rules of Hooks apply to it.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Progress Bar:
&lt;/h2&gt;

&lt;p&gt;Let's see I wanted to build a progress bar component that will get completed in a specified amount of time. for that, I will need some sort of a variable whose value increase after every second and stops after a specified.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--BTrk7b6p--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://freefrontend.com/assets/img/css-progress-bars/Loading-Bar.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--BTrk7b6p--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://freefrontend.com/assets/img/css-progress-bars/Loading-Bar.gif" alt="Untitled" width="880" height="498"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Counter Custom Hook:
&lt;/h3&gt;

&lt;p&gt;Let’s first create a counter hook that will return a value that will get updated after every second.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// 📁 Timer.js&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useEffect&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&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;useCounter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;start&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;end&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;reach&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setReach&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="nx"&gt;start&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nx"&gt;useEffect&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="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;myInterval&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;setInterval&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;setReach&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;prev&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;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;prev&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;end&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;clearInterval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;myInterval&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;++&lt;/span&gt;&lt;span class="nx"&gt;prev&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="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="p"&gt;[]);&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;reach&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;useCounter&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As a said before hooks are just the fancy name for javascript functions. In the above code first, we have created a &lt;code&gt;reach&lt;/code&gt; state which tells us where the counter has reached or at which value it's at. and then we have &lt;code&gt;useEffect&lt;/code&gt;it runs only one’s and run a &lt;code&gt;setInterval&lt;/code&gt; function which takes a callback as a parameter inside we have our &lt;code&gt;setReach&lt;/code&gt; function which increments a previous value after each second and when it hits the limit it calls the &lt;code&gt;clearInterval&lt;/code&gt; function which stops the interval. And in the end, we return the &lt;code&gt;reach&lt;/code&gt; variable to use in other components.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;More on &lt;strong&gt;setInterval&lt;/strong&gt; &lt;a href="https://www.w3schools.com/jsref/met_win_setinterval.asp"&gt;here&lt;/a&gt;*&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  How to use a Custom Hook:
&lt;/h3&gt;

&lt;p&gt;We can use our &lt;code&gt;useCounter&lt;/code&gt; hook the same as any other hook inside our react application. for the progress bar, we will use &lt;code&gt;HTML's&lt;/code&gt; own &lt;code&gt;progress&lt;/code&gt; tag to represent the progress.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;More on &lt;strong&gt;progress tag&lt;/strong&gt; &lt;a href="https://www.w3schools.com/tags/tag_progress.asp"&gt;here&lt;/a&gt;&lt;/p&gt;


&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// 📁 App.js&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;useCounter&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./Timer&lt;/span&gt;&lt;span class="dl"&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="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useCounter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;60&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="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;progress&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="na"&gt;max&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"60"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;progress&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&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;&lt;code&gt;useCounter&lt;/code&gt; requires two parameters one for the initial value where to start and one is for where to end.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;That's pretty much it's about creating your own custom hooks in react custom hooks are just functions and they are easier than you think. Custom React hooks can give us the tools to fix our own problems when third-party libraries fall short. &lt;/p&gt;

&lt;p&gt;Also, &lt;a href="https://github.com/streamich/react-use"&gt;react-use&lt;/a&gt; is also a very useful library that provides bundles of custom hooks that you can use in your application. Right out of the box you get so many custom hooks ready to use. Which has hooks for sensors, UI, animation, states, and many more. do check that out.&lt;/p&gt;

&lt;p&gt;I hope this guide helps you in some way. If it did leave a comment follow my profile for more content like this or you read some of my other articles and if you have any confusion ping me on &lt;a href="https://www.linkedin.com/in/shahabbukhari/"&gt;Linkedin&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;THANKS FOR READING&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>React Pre-Requisite: JS Must Know Concepts Before Learning React</title>
      <dc:creator>shahabbukhari</dc:creator>
      <pubDate>Tue, 19 Apr 2022 23:06:14 +0000</pubDate>
      <link>https://dev.to/shahabbukhari/react-pre-requisite-js-must-know-concepts-before-learning-react-2d08</link>
      <guid>https://dev.to/shahabbukhari/react-pre-requisite-js-must-know-concepts-before-learning-react-2d08</guid>
      <description>&lt;p&gt;Many of you learned javascript and wanted to learn to react to the most popular library for javascript in the market as well as in the community right now. and according to the &lt;a href="https://insights.stackoverflow.com/survey/2021#technology-most-popular-technologies"&gt;Stack Overflows 2021 survey&lt;/a&gt; React is the most wanted and desired library by one in four developers.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;
Must-Know Javascript Concepts:

&lt;ul&gt;
&lt;li&gt;Let and Const&lt;/li&gt;
&lt;li&gt;Template Literals&lt;/li&gt;
&lt;li&gt;Destructing&lt;/li&gt;
&lt;li&gt;Spread Operator&lt;/li&gt;
&lt;li&gt;THIS&lt;/li&gt;
&lt;li&gt;Arrow Functions&lt;/li&gt;
&lt;li&gt;
Modules
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;
Basic of Functional Programming:

&lt;ul&gt;
&lt;li&gt;Immutability&lt;/li&gt;
&lt;li&gt;Higher-Order Functions&lt;/li&gt;
&lt;li&gt;Pure Functions&lt;/li&gt;
&lt;li&gt;Recursion&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Must-Know Javascript Concepts:
&lt;/h2&gt;

&lt;p&gt;React makes it painless to create interactive UIs user interface-based components. But. But. Before building a complex skyscraper with the help of React we should dig deeper in the daunting caves of javascript to build a foundation to build these complex web apps.&lt;br&gt;
As a famous saying is &lt;strong&gt;&lt;em&gt;&lt;code&gt;“don’t reinvent the wheel”&lt;/code&gt;&lt;/em&gt;&lt;/strong&gt; There's certainly truth there, but I think it's a bit dangerous if taken too literally for me the reinvented version of the quote will be &lt;strong&gt;&lt;em&gt;&lt;code&gt;“Don't Reinvent The Wheel Unless You Plan on Learning More About Wheels”&lt;/code&gt;.&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
Before getting emersed in the world of reacting there are some pre-requisites or core concepts and different styles of programming in javascript and you should have a better understanding of it before learning to react.&lt;/p&gt;
&lt;h3&gt;
  
  
  Let and Const:
&lt;/h3&gt;

&lt;p&gt;With ES6 javascript surprise us with the new way to declare variables in javascript with &lt;code&gt;let&lt;/code&gt;&lt;br&gt;
and &lt;code&gt;const&lt;/code&gt; in contrast to the old way of declaring a variable with &lt;code&gt;var&lt;/code&gt; introducing let and cons was issues developers had to face with scoping and hoisting in &lt;code&gt;var&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Let:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;let&lt;/code&gt; variables are scoped to the immediate &lt;em&gt;enclosing&lt;/em&gt; block denoted by &lt;code&gt;{}&lt;/code&gt; (hence the block scope). Variables declared with &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; are also hoisted but, unlike &lt;code&gt;var&lt;/code&gt;, are not initialized with a default value. An exception will be thrown if a variable declared with &lt;code&gt;let&lt;/code&gt; or &lt;code&gt;const&lt;/code&gt; is read before it is initialized.&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;// Hoisting&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;num&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Throws ReferenceError exception as the variable value is uninitialized&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Initialization&lt;/span&gt;
&lt;span class="c1"&gt;// Scope&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Shahab&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;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// Shahab&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;myName&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;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Ali&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;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// Ali&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;myName&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;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// Shahab&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Const:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const&lt;/code&gt; declarations share some similarities with &lt;code&gt;let&lt;/code&gt; declarations. the only difference is the value assigned with &lt;code&gt;const&lt;/code&gt; remains the same within its 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;const&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Shahab&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Ali&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="c1"&gt;// error: Assignment to constant variable.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Note:&lt;/code&gt; Every &lt;code&gt;const&lt;/code&gt; declaration, therefore, must be initialized at the time of declaration.&lt;/p&gt;

&lt;p&gt;For More In-depth analysis of &lt;code&gt;scopes&lt;/code&gt;, &lt;code&gt;hoisting&lt;/code&gt; and &lt;code&gt;Coercion&lt;/code&gt; check my other article.&lt;/p&gt;

&lt;h3&gt;
  
  
  Template Literals:
&lt;/h3&gt;

&lt;p&gt;Template literal provides an easy way to insert multiple variables and expressions into strings allowing for multi-line strings, for string interpolation.&lt;/p&gt;

&lt;p&gt;Template literals are enclosed by backtick &lt;code&gt;(&lt;/code&gt;&lt;code&gt;)&lt;/code&gt; characters instead of double &lt;code&gt;(")&lt;/code&gt; or single quotes&lt;code&gt;(')&lt;/code&gt;. And &lt;code&gt;${...}&lt;/code&gt; as a string place holder to insert variables or expressions in it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;firstName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Shahab&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;lastName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Bukhari&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="s2"&gt;`My name is &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// My name is Shahab Bukhari&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Destructing:
&lt;/h3&gt;

&lt;p&gt;ES6 comes with Destructuring assignment a new way of unpacking desired values from an array of properties from an object and assigning them into distinct variables.&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;// Array&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;topStudents&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Jhon Doe&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Alan Turing&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Einstein&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hawking&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Newton&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;firstSTD&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;secondSTD&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;thirdSTD&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;topStudents&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;firstSTD&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;secondSTD&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;thirdSTD&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;span class="c1"&gt;//Jhon Doe, Alan, Turing Einstein&lt;/span&gt;

&lt;span class="c1"&gt;// Object&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;myPhone&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;model&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Iphone 13 Pro Max&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;year&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2021&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;camera&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;12-megapixel&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;price&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="kd"&gt;let&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;model&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;price&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;myPhone&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="s2"&gt;`I have &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;model&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; which cost around &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;price&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;$`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;//i have Iphone 13 Pro Max which cost around 1000$&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also skip some items in an array. What if you only want first and last student in.&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;let&lt;/span&gt; &lt;span class="nx"&gt;topStudents&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Jhon Doe&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Alan Turing&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Einstein&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hawking&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Newton&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;firstSTD&lt;/span&gt; &lt;span class="p"&gt;,,,,&lt;/span&gt; &lt;span class="nx"&gt;lastSTD&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;topStudents&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;firstSTD&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;lastSTD&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Jhon Doe, Newton&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The comma separator is used to skip values in an array. So if you want to skip an item in an array, just use a comma.&lt;/p&gt;

&lt;h3&gt;
  
  
  Spread Operator:
&lt;/h3&gt;

&lt;p&gt;The Spread Operator is three dots &lt;code&gt;(...)&lt;/code&gt; that is used to perform several tasks. The spread operator allows us to quickly copy all or part of an existing array or object into another array or object. As a name suggests it spreads the elements of an array or object in another array or objects.&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;let&lt;/span&gt; &lt;span class="nx"&gt;oddNum&lt;/span&gt; &lt;span class="o"&gt;=&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;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;evenNum&lt;/span&gt; &lt;span class="o"&gt;=&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="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="nx"&gt;evenNum&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;oddNum&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="c1"&gt;//[  1, 2, 3, 4, 5, 6, 7, 8, 9 ]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ok, cool why do we need it. The spread operator helps us deal with the problem of &lt;code&gt;immutability&lt;/code&gt;(which we will be covering in the Functional Programming Section extensively). &lt;/p&gt;

&lt;h3&gt;
  
  
  THIS:
&lt;/h3&gt;

&lt;p&gt;In JavaScript, &lt;code&gt;this&lt;/code&gt; keyword refers to an &lt;strong&gt;object&lt;/strong&gt;. &lt;strong&gt;Which&lt;/strong&gt; object depends on how &lt;code&gt;this&lt;/code&gt; is being invoked &lt;em&gt;(used or called).&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;For the sake of simplicity, let's discuss the two main Cases.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;When used in a normal function:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When &lt;code&gt;this&lt;/code&gt; is used in a normal function it refers to the &lt;code&gt;window&lt;/code&gt; object or &lt;code&gt;global&lt;/code&gt; in node.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;hello&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;a&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="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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;hello&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="c1"&gt;// Window {window: Window, self: Window, document: document, name: '', location: Location, …}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;When used as a method in an object:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When &lt;code&gt;this&lt;/code&gt; is used as a method in an object it refers to the object from which its executing.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ohh&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="s2"&gt;shahab&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;say&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="k"&gt;this&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;ohh&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;say&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="c1"&gt;// {name: 'shahab', say: ƒ}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Arrow Functions:
&lt;/h3&gt;

&lt;p&gt;Arrow functions are a useful new feature of ES6 An &lt;strong&gt;arrow function expression&lt;/strong&gt; is a compact alternative to a traditional function expression, but arrow functions are limited and can't be used in all situations.&lt;/p&gt;

&lt;p&gt;Let's write a function that returns a square of any number.&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;// Old Way&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;square&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="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;return&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;a&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;// Modern Way&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;square&lt;/span&gt; &lt;span class="o"&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="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="nx"&gt;a&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="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;square&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;// 4&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It can get shorter. If a function is only accepting one argument then you can remove the &lt;code&gt;(&lt;/code&gt; parenthesis and if the function has only one statement you can also remove the &lt;code&gt;{&lt;/code&gt; braces and the function returns the statement by default. Now, let's rewrite the function.&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;square&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;a&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;a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Note:&lt;/code&gt; Remember I said, “Arrow Functions are limited and can't be used in all situations.” one of the places where you should not use the arrow function is with &lt;code&gt;this&lt;/code&gt; keyword. And can cause difficult to track errors. the reason behind this is &lt;em&gt;Arrow functions do not bind their own &lt;code&gt;this&lt;/code&gt;&lt;br&gt;
, instead, they inherit the one from the parent scope&lt;/em&gt;. &lt;br&gt;
Let's take an example where the arrow function is used as a method in 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;myObject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;myMethod&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="k"&gt;this&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;myObject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;myMethod&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;// window or global object&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Weird! remember as we discussed arrow functions do not bind their own &lt;code&gt;this&lt;/code&gt; they inherit it from the parent which in this case is the window or global object.&lt;/p&gt;

&lt;h3&gt;
  
  
  Modules:
&lt;/h3&gt;

&lt;p&gt;When you are working in react or any complex type of application you split your code into multiple files called modules or components in react.js.&lt;/p&gt;

&lt;p&gt;A Module is just a reusable code/file that performs a certain task that we have isolated from the rest of our code for managing the complexity of the application. Before ES6 the only way to work with modular javascript was external libraries but now things have been changed ES6 itself supports modules.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;Export&lt;/code&gt;: &lt;code&gt;export&lt;/code&gt; can be used to make something visible outside the module/file.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// 📁 Info.js&lt;/span&gt;
&lt;span class="c1"&gt;// In-Line Individually&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Shahab&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;course&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Computer Science&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// All at once at bottom&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;course&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;If you are only exporting one variable you can use &lt;code&gt;export default&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// 📁 Info.js&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;fullName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; Bukhari`&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;fullName&lt;/span&gt; &lt;span class="c1"&gt;// only one variable can be default exported&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;both &lt;code&gt;export&lt;/code&gt; and &lt;code&gt;export default&lt;/code&gt; can be used in the same file.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;Import&lt;/code&gt;: &lt;code&gt;import&lt;/code&gt; can be used to import exported variables from a module/file to use in a current file.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// 📁 main.js&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;course&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./Info.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;fullName&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./Info.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;Multiple modules exported with export keyword can be accessed through object destructing and any module exported with export default keyword can be imported as a single variable. and now you can use these variables in your current file/module.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// 📁 main.js&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;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;course&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// Shahab, Computer Science&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;fullName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// Shahab Bukhari&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Basic of Functional Programming:
&lt;/h2&gt;

&lt;p&gt;Functional Programming is a programming paradigm or you can say it's a way of thinking in which we try to build programs by &lt;code&gt;composing pure functions&lt;/code&gt;(We will get back to pure functions). Understanding the basic concepts of functional programming is a valuable skill for any react developer. &lt;/p&gt;

&lt;p&gt;In javascript functions are first-class citizens which means &lt;strong&gt;you can store functions in variables, pass them to other functions as arguments, and return them from other functions as values.&lt;/strong&gt; you declare a function with var, let, or const keyword as you declare any other variable.&lt;/p&gt;

&lt;p&gt;Functional programming is a part of a larger programming paradigm: &lt;code&gt;declarative programming&lt;/code&gt;. Declarative programming is a style of programming where applications are structured in a way that prioritizes describing what should happen over defining how it should happen.&lt;/p&gt;

&lt;p&gt;Now let's discuss some core concepts of functional programming.&lt;/p&gt;

&lt;h3&gt;
  
  
  Immutability:
&lt;/h3&gt;

&lt;p&gt;In a literal sense, Immutability means unchangeable. In a paradigm of functional programming, after initializing an object we do not change its values or its state. Think of immutability as “save as” because you know it returns a newly changed object while traditional in-place mutation would be like “save” which means updating the original and letting go of an earlier state. &lt;/p&gt;

&lt;p&gt;Immutability gives stricter control over your data immediately making your code safer and more predictable. &lt;/p&gt;

&lt;p&gt;before this let's understand the problem of why we need immutability. There are two types of data in javascript primitive (pass by value) and non-primitive(pass by reference). &lt;br&gt;
Javascript has 5 data types that are passed by &lt;strong&gt;&lt;em&gt;value&lt;/em&gt;&lt;/strong&gt;: &lt;code&gt;Boolean&lt;/code&gt;, &lt;code&gt;null&lt;/code&gt;, &lt;code&gt;undefined&lt;/code&gt;, &lt;code&gt;String&lt;/code&gt;, and &lt;code&gt;Number&lt;/code&gt;. We’ll call these &lt;strong&gt;primitive types&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Javascript has 3 data types that are passed by &lt;strong&gt;&lt;em&gt;reference&lt;/em&gt;&lt;/strong&gt;: &lt;code&gt;Array&lt;/code&gt;, &lt;code&gt;Function&lt;/code&gt;, and &lt;code&gt;Object&lt;/code&gt;. These are all technically Objects, so we’ll refer to them collectively as &lt;strong&gt;Objects&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Primitive types are easier to make immutable because when you assign a primitive data type to another variable it stores its value.&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;let&lt;/span&gt; &lt;span class="nx"&gt;myName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;shahab&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;newName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;myName&lt;/span&gt;
&lt;span class="nx"&gt;newName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;shahab bukhari&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;myName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// shahab&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;newName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// shahab bukhari&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;but in terms of non-primitive type data is passed as a reference that's because when variables that are assigned a non-primitive value are given a &lt;em&gt;reference&lt;/em&gt; to that value. That reference points to the object’s location in memory. The variables don’t actually contain the value.&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;let&lt;/span&gt; &lt;span class="nx"&gt;even1to10&lt;/span&gt; &lt;span class="o"&gt;=&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="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;even1to20&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;even1to10&lt;/span&gt;
&lt;span class="nx"&gt;even1to20&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;even1to20&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;14&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;even1to20&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;even1to20&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;18&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;even1to20&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;20&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;even1to10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// [2,4,6,8,10,12,14,16,18,20]&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;even1to20&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// [2,4,6,8,10,12,14,16,18,20]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both of the arrays have the same values. because array &lt;code&gt;even1to20&lt;/code&gt; store the reference to the same location array &lt;code&gt;even1to10&lt;/code&gt; pointing and save the changes on that. That's the classic example of why immutability is important. &lt;/p&gt;

&lt;p&gt;luckily we can solve this issue with the &lt;code&gt;spread operator&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;let&lt;/span&gt; &lt;span class="nx"&gt;even1to10&lt;/span&gt; &lt;span class="o"&gt;=&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="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;even1to20&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="nx"&gt;even1to10&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="nx"&gt;even1to20&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;even1to20&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;14&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;even1to20&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;even1to20&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;18&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;even1to20&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;20&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;even1to10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// [2,4,6,8,10]&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;even1to20&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// [2,4,6,8,10,12,14,16,18,20]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's how we store the values/state of an object/array in a new object/array and then modify it to ensure immutability.&lt;/p&gt;

&lt;h3&gt;
  
  
  Higher-Order Functions:
&lt;/h3&gt;

&lt;p&gt;Higher-order functions are functions that can manipulate other functions. They can&lt;br&gt;
take functions as arguments, return functions, or both.&lt;br&gt;
Some of the built-in higher-order functions in javascript are. Array.map, Array.filter, and Array.reduce all take functions as arguments.&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;calculate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;operation&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;initialValue&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;total&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;initialValue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&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;number&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;total&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;operation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;total&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;number&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;total&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;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;n2&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;n1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;n2&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;multiply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;n2&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;n1&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;n2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;calculate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&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="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;      &lt;span class="c1"&gt;// 7&lt;/span&gt;
&lt;span class="nx"&gt;calculate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;multiply&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="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="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt; &lt;span class="c1"&gt;// 8&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here &lt;em&gt;&lt;code&gt;calculate(operation, initialValue, numbers)&lt;/code&gt; is a higher-order function because it accepts a function as the first argument.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;calculate(operation, initialValue, numbers)&lt;/code&gt; accepts the first argument a function that describes the operation, the second argument is the initial value, and finally, the third argument is the array of numbers. and then returns the results.&lt;/p&gt;

&lt;h3&gt;
  
  
  Pure Functions:
&lt;/h3&gt;

&lt;p&gt;A pure function is a function that returns a value that is computed based on its argu‐&lt;br&gt;
ments. Pure functions take at least one argument and always return a value or&lt;br&gt;
another function. They do not cause side effects, set global variables, or change any‐&lt;br&gt;
the thing about the application state. They treat their arguments as immutable data.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A function is only pure if, given the same input, it will always produce the same output&lt;/strong&gt;. When testing a pure function, you control the arguments, and thus you can estimate the outcome.&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;highpass&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cutoff&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;value&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;value&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="nx"&gt;cutoff&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;highpass&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;123&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;span class="nx"&gt;highpass&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&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="c1"&gt;// false&lt;/span&gt;
&lt;span class="nx"&gt;highpass&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;   &lt;span class="c1"&gt;// false&lt;/span&gt;
&lt;span class="nx"&gt;highpass&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;   &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Recursion:
&lt;/h3&gt;

&lt;p&gt;Recursion happens when a function calls itself. Code using recursion is usually shorter and more elegant. Sequence generation is cleaner with recursion than with iteration. &lt;/p&gt;

&lt;p&gt;There are two components of recursion.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;**Recursive Case:**&lt;/code&gt; where function calls itself with different arguments if the arguments are the same then it will get stuck in an infinite state. y&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;Base Case:&lt;/code&gt;&lt;/strong&gt; The last component of a useful recursive function is the &lt;em&gt;base case&lt;/em&gt;. The base case is a condition in which a function can return the result without another call to itself&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The classic example of recursion where recursion is the best solution: is the Fibonacci sequence.&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;Fibonacci&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&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="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;1&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;num&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;Fibonacci&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;Fibonacci&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&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;Fibonacci&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;// 21&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Congratulations🥳🥳:
&lt;/h2&gt;

&lt;p&gt;Thanks for reading if you stick till the end and I hope this helped you understand core concepts of javascript which will 100% help you learn react more effectively. If not, try reading it again, and again… until you reach your base case!&lt;br&gt;
For more articles like this you can follow me on &lt;a href="https://dev.to/shahabbukhari"&gt;dev.to&lt;/a&gt; and connect with me on &lt;a href="https://www.linkedin.com/in/shahabbukhari/"&gt;LinkedIn&lt;/a&gt;:&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Javascript the Confusing Parts: Part 1 Coercion, Let vs Const vs var</title>
      <dc:creator>shahabbukhari</dc:creator>
      <pubDate>Tue, 15 Feb 2022 22:26:04 +0000</pubDate>
      <link>https://dev.to/shahabbukhari/javascript-the-confusing-parts-part-1-coercion-let-vs-const-vs-var-i6m</link>
      <guid>https://dev.to/shahabbukhari/javascript-the-confusing-parts-part-1-coercion-let-vs-const-vs-var-i6m</guid>
      <description>&lt;p&gt;Javascript is one of the most popular languages on the internet. According to the StackOverflow, 2021 survey javascript is the most popular programming/scripting language in 2021. &lt;/p&gt;

&lt;p&gt;Some People say javascript is a weird programming language sometimes it's true. In particular, because JavaScript has C/C++/Java-like syntax, which causes such developers to assume that it has similar semantics as well. When this turns out not to be the case, such developers often feel confused and frustrated. That said, for a person who is new to development, without prior experience in some other programming language, I don’t think JavaScript would seem stranger than most other languages that person might look at.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.postimg.cc%2FxTPknqsM%2Fbest-js-meme-to-date-2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.postimg.cc%2FxTPknqsM%2Fbest-js-meme-to-date-2.png" alt="Untitled"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this series of articles, we will dive deep into the weird parts of javascript and try to understand how javascript actually works under the hood.&lt;/p&gt;

&lt;p&gt;Javascript is not a terribly written language it just abstracts you away from how computer or browser that you will be using to run javascript. For this, we need to understand how these tools work. you may say you don’t need to understand these concepts for working with javascript but as we go along you will understand these fundamental concepts will help you have a concrete understanding of the concept and surely you will have some aha moments. and understanding these fundamental parts differentiate between good and average developers.&lt;/p&gt;

&lt;h2&gt;
  
  
  Coercion:
&lt;/h2&gt;

&lt;p&gt;Starting from the meme. Coercion refers to the process of automatic or implicit conversion of values from one data type to another. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.postimg.cc%2FryNyt3SL%2Fpanel-1-1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.postimg.cc%2FryNyt3SL%2Fpanel-1-1.png" alt="Untitled"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;How on earth &lt;code&gt;0 == "0"&lt;/code&gt; when 0 is an int and “0” is string. Welcome to the javascript world. In the world of javascript when you compare two values with &lt;code&gt;==&lt;/code&gt; one value may go through coercion. JavaScript is a weakly-typed language, values can also be converted between different types automatically, and it is called &lt;strong&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/Type_coercion" rel="noopener noreferrer"&gt;implicit type coercion&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.postimg.cc%2FVkxPhWjR%2Fpanel-2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.postimg.cc%2FVkxPhWjR%2Fpanel-2.png" alt="Untitled"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This just didn't stop here arrays(which is non-primitive in nature) also coerce into a string (primitive type). Since &lt;code&gt;[]&lt;/code&gt; is an empty array hence it gets converted into an empty string.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.postimg.cc%2FcJR2by1x%2Fpanel-4-1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.postimg.cc%2FcJR2by1x%2Fpanel-4-1.png" alt="Untitled"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But But But if &lt;code&gt;0 == "0"&lt;/code&gt; is true and &lt;code&gt;0 == []&lt;/code&gt; is true then why &lt;code&gt;"0" == []&lt;/code&gt; is false. thsi part will make sense if you understood the first two parts. &lt;br&gt;
&lt;code&gt;"0"&lt;/code&gt; is a string and &lt;code&gt;[]&lt;/code&gt; is an object(that is another story why) then if your convert &lt;code&gt;[]&lt;/code&gt; to string it will become &lt;code&gt;""&lt;/code&gt; an empty string. So &lt;code&gt;"0" == ""&lt;/code&gt; is false.&lt;/p&gt;
&lt;h2&gt;
  
  
  2. Let Vs Const Vs Var:
&lt;/h2&gt;

&lt;p&gt;With ES6 javascript surprise us with the new way to declaring variable in javascript with &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; in contrast to the old way of decllearing a variable with &lt;code&gt;var&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Before starting a war between let, const and var first you need to understand two concpets &lt;code&gt;Scope&lt;/code&gt; and &lt;code&gt;Hoisting&lt;/code&gt; ?&lt;/p&gt;
&lt;h3&gt;
  
  
  Scope:
&lt;/h3&gt;

&lt;p&gt;I would say that scope is the ‘environment’ in which a variable is accessible and visible and can be used. there are three type of scopes in javascript.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Global Scope:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When we declare a variable on our file, out &lt;strong&gt;of any function&lt;/strong&gt; this is the &lt;strong&gt;global scope,&lt;/strong&gt; so the variable is global, &lt;em&gt;it is accessible and visible everywhere&lt;/em&gt; and it &lt;strong&gt;becomes property of the global object&lt;/strong&gt;, e.g. window.&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;myVar&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 World&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;




&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Local Scope:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Local variables are variables that are declared inside a function and they live and die when this function is executed.They are not available/accessible/visible outside of the function that it is created. For that reason, we can have many local variables with the same name without having any impact on the rest code.&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Shahab&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="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// Shahab&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;myName&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;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Ali&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="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// Ali&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nf"&gt;myName&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="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// Shahab&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;




&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Block Scope:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Local Variables are crreated inside function block level variables live and die inside a bock of code, like &lt;code&gt;{}&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Variables decleared with &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt;are block scoped.&lt;/p&gt;


&lt;/blockquote&gt;

&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;sayHello&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;flag&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;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;shahab&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// local variable&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;flag&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;city&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;karachi&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// block scope variable, only accessible inside this if&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;`My name is &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;, its cold in &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;city&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&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="s2"&gt;`My name is &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;, its cold in &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;city&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// &lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;sayHello&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="c1"&gt;// "My name is shahab, its cold in karachi"&lt;/span&gt;
&lt;span class="nf"&gt;sayHello&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="c1"&gt;// Uncaught ReferenceError: city is not defined&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;




&lt;/li&gt;

&lt;/ol&gt;

&lt;h3&gt;
  
  
  Hoisting:
&lt;/h3&gt;

&lt;p&gt;JavaScript &lt;strong&gt;Hoisting&lt;/strong&gt; refers to the process whereby the interpreter appears to move the &lt;em&gt;declaration&lt;/em&gt; of functions, variables or classes to the top of their scope &lt;em&gt;within the scope&lt;/em&gt; regardless of where they are decleared, prior to execution of the code. Hoisting allows functions to be safely used in code before they are declared.&lt;/p&gt;

&lt;p&gt;Now back to the original disccussion of let vs const vs var. lets compare them one by one.&lt;/p&gt;

&lt;h3&gt;
  
  
  Var
&lt;/h3&gt;

&lt;p&gt;Variables declared by &lt;code&gt;var&lt;/code&gt; keyword are scoped to the immediate function body (hence the function/local scope) and the varibles decleared with var are hoisted top of the program and can be used before initalizing.&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;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Returns 'undefined' from hoisted var declaration (not 6)&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Declaration&lt;/span&gt;
&lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Initialization&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Returns 6 after the line with initialization is executed.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Let:
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;let&lt;/code&gt; variables are scoped to the immediate &lt;em&gt;enclosing&lt;/em&gt; block denoted by &lt;code&gt;{}&lt;/code&gt; (hence the block scope). Variables declared with &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; are also hoisted but, unlike &lt;code&gt;var&lt;/code&gt;, are not initialized with a default value. An exception will be thrown if a variable declared with &lt;code&gt;let&lt;/code&gt; or &lt;code&gt;const&lt;/code&gt; is read before it is initialized.&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;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Throws ReferenceError exception as the variable value is uninitialized&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Initialization&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Const:
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;const&lt;/code&gt;declarations share some similarities with &lt;code&gt;let&lt;/code&gt;declarations. the only differnce is the value assigned with &lt;code&gt;const&lt;/code&gt; remains same within its 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;const&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Shahab&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Ali&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="c1"&gt;// error: Assignment to constant variable.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every &lt;code&gt;const&lt;/code&gt; declaration, therefore, must be initialized at the time of declaration.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;Note:&lt;/code&gt; I said values assigned with const cannot be changed. in case of array and object(which are same we will learn in upcoming lectures) can be updated because object decleared with const does not store a values it stores a memory address(pointer) of those values any changes we make will on a memory address.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  But why let and const existed...
&lt;/h2&gt;

&lt;p&gt;The reason why &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; keyword was introduced to the language because of two main reason. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Function/Local scope is confusing and was one of the main sources of bugs in JavaScript. &lt;/li&gt;
&lt;li&gt;Hoisting in JavaScript cause unexpected results.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Issue Caused by local scope:
&lt;/h3&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;var&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&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;for &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;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&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;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&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;hello world&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="c1"&gt;// hello world&lt;/span&gt;
&lt;span class="c1"&gt;// hello world&lt;/span&gt;
&lt;span class="c1"&gt;// hello world&lt;/span&gt;
&lt;span class="c1"&gt;// hello world&lt;/span&gt;
&lt;span class="c1"&gt;// hello world&lt;/span&gt;
&lt;span class="c1"&gt;// &amp;lt;repeats infinitely&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;you can solve this issue by just using &lt;code&gt;let&lt;/code&gt;. which will convert &lt;code&gt;i&lt;/code&gt;into block scope and the refrence will be different in both for loops.&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;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&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;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&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;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&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;hello world&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="c1"&gt;// hello world&lt;/span&gt;
&lt;span class="c1"&gt;// hello world&lt;/span&gt;
&lt;span class="c1"&gt;// hello world&lt;/span&gt;
&lt;span class="c1"&gt;// hello world&lt;/span&gt;
&lt;span class="c1"&gt;// hello world&lt;/span&gt;
&lt;span class="c1"&gt;// hello world&lt;/span&gt;
&lt;span class="c1"&gt;// hello world&lt;/span&gt;
&lt;span class="c1"&gt;// hello world&lt;/span&gt;
&lt;span class="c1"&gt;// hello world&lt;/span&gt;
&lt;span class="c1"&gt;// hello world&lt;/span&gt;
&lt;span class="c1"&gt;// hello world&lt;/span&gt;
&lt;span class="c1"&gt;// hello world&lt;/span&gt;
&lt;span class="c1"&gt;// hello world&lt;/span&gt;
&lt;span class="c1"&gt;// hello world&lt;/span&gt;
&lt;span class="c1"&gt;// hello world&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Unexpected Results Cause By Hoisting:
&lt;/h2&gt;

&lt;p&gt;The tricky part is that only the declaration of the variable will be hoisted. If the variable is also initialized, the variable will be set to undefined when it is hoisted to the top. The variable value is changed from undefined to its initialized value when the execution reaches the variable.&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;firstName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Shahab&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="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;firstName&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt; &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;lastName&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;lastName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Bukhari&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Shahab Undefined&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will not through an error but rendered it as default value of undefined that is even worse. to avoid this we use &lt;code&gt;let&lt;/code&gt; or &lt;code&gt;const&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;let&lt;/span&gt; &lt;span class="nx"&gt;firstName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Jennifer&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="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;firstName&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt; &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;lastName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Bland&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;//Cannot access 'lastName' before initialization&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will help you to avoid errors before publishing your code publicaly.&lt;/p&gt;

&lt;h2&gt;
  
  
  Thanks of Reading:
&lt;/h2&gt;

&lt;p&gt;If you read this so far surely you find this intresting and if you learn something consider following me. I will be continoning this series of Javacript the confusing parts follow me not to miss any future updates. Thanks&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Git Simplified: Working Collaboratively with Git/Github</title>
      <dc:creator>shahabbukhari</dc:creator>
      <pubDate>Tue, 12 Oct 2021 17:18:12 +0000</pubDate>
      <link>https://dev.to/shahabbukhari/git-simplified-working-collaboratively-with-gitgithub-5349</link>
      <guid>https://dev.to/shahabbukhari/git-simplified-working-collaboratively-with-gitgithub-5349</guid>
      <description>&lt;p&gt;This is the follow-up article of &lt;a href="https://dev.to/shahabbukhari/git-simplified-a-beginner-guide-to-git-and-github-19o8"&gt;Git Simplified: A Beginner guide to git and GitHub&lt;br&gt;
&lt;/a&gt;. In that article, I have explained basic git terminologies, workflow, and some basic git commands from initializing a project on your computer/laptop to publishing to Github. If you are a beginner I will highly recommend you to do check &lt;a href="https://dev.to/shahabbukhari/git-simplified-a-beginner-guide-to-git-and-github-19o8"&gt;it&lt;/a&gt; first.&lt;/p&gt;
&lt;h2&gt;
  
  
  Working Collaboratively:
&lt;/h2&gt;

&lt;p&gt;Working in a team and maintaining a code base and tracking the changes of each and everyone &lt;strong&gt;manually&lt;/strong&gt; is hard(and a terrible choice). This is just one of many ways to collaborate on a project using GitHub. But it’s one I would suggest if you’re just starting out working with a team and haven’t established a git-flow yet or know where to start in establishing one.&lt;/p&gt;
&lt;h3&gt;
  
  
  Get a copy:
&lt;/h3&gt;

&lt;p&gt;Most of the time if you working on a large-scale/open-source project you don't have write access to the code. for working on your desired project first you have to fork the project into your remote repository.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--R39xOTwB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://imgur.com/RllznR1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--R39xOTwB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://imgur.com/RllznR1.png" alt="" width="757" height="93"&gt;&lt;/a&gt;&lt;br&gt;
Now first fork a project. what it does is create a copy of the original project into your repositories.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--HaJ0hyiJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://imgur.com/bkXig50.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--HaJ0hyiJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://imgur.com/bkXig50.png" alt="" width="522" height="72"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Setup a local copy:
&lt;/h3&gt;

&lt;p&gt;Now clone a repository into your local machine.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git clone &amp;lt;remote repo &lt;span class="nb"&gt;link&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And if you forked the repository and you don't have write permission there is another step you have taken.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git remote add &amp;lt;remote name &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="nb"&gt;source &lt;/span&gt;repo&amp;gt; &amp;lt;&lt;span class="nb"&gt;source &lt;/span&gt;repository clone &lt;span class="nb"&gt;link&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Update a local copy:
&lt;/h3&gt;

&lt;p&gt;When you are working in a team you are required to update your local copy with the changes your partners/colleagues have made.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git pull &amp;lt;remote name&amp;gt; &amp;lt;branch name&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;git pull&lt;/strong&gt; is a Git command used to update the local version of a repository from a remote. &lt;strong&gt;git pull&lt;/strong&gt; is a combination command, equal to &lt;strong&gt;git fetch&lt;/strong&gt; &lt;em&gt;+&lt;/em&gt; &lt;strong&gt;git merge&lt;/strong&gt;.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--hDQiRqwP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.stack.imgur.com/nWYnQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hDQiRqwP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.stack.imgur.com/nWYnQ.png" alt="" width="880" height="385"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;Fetch&lt;/em&gt;&lt;/strong&gt; the changes from remote repositories and store them in your local repository.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;Marge&lt;/em&gt;&lt;/strong&gt; those changes store in your local repository with your working directory&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can achieve the same result in two different steps with!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git fetch &amp;lt;remote name&amp;gt; &amp;lt;branch name&amp;gt;
git merge &amp;lt;remote&amp;gt;/&amp;lt;branch&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Compare changes:
&lt;/h3&gt;

&lt;p&gt;As we know git tracks all the changes you have made. If you want to compare your changes in &lt;strong&gt;working directory&lt;/strong&gt; with &lt;strong&gt;staging area&lt;/strong&gt;, &lt;strong&gt;local repository&lt;/strong&gt; or even you can compare your changes with two different &lt;strong&gt;branches&lt;/strong&gt; in git with &lt;strong&gt;git diff&lt;/strong&gt; command.&lt;br&gt;
Let's take an example from our previous task when we &lt;strong&gt;fetched&lt;/strong&gt; the data from the remote repository in our &lt;strong&gt;local repository&lt;/strong&gt;. &lt;br&gt;
For this purpose, I have made a repository &lt;a href="https://github.com/shahabbukhari/git-test"&gt;git-test&lt;/a&gt; in my GitHub and &lt;strong&gt;cloned&lt;/strong&gt; that repo in my local machine.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git clone https://github.com/shahabbukhari/git-test.git
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After that, I made some changes in &lt;strong&gt;README. MD&lt;/strong&gt; file remotely. now i will fetch the changes&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git fetch origin master
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--aTm2yiAf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://imgur.com/Qlstrdq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--aTm2yiAf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://imgur.com/Qlstrdq.png" alt="" width="759" height="176"&gt;&lt;/a&gt;&lt;br&gt;
Now I have a change from &lt;strong&gt;remote repo&lt;/strong&gt; in my &lt;strong&gt;local repo&lt;/strong&gt; (repo aka repository). Now, let's compare those changes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git diff origin/master
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ULW8Hp8c--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://imgur.com/uhhnO4Y.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ULW8Hp8c--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://imgur.com/uhhnO4Y.png" alt="" width="807" height="192"&gt;&lt;/a&gt;&lt;br&gt;
I know it looks scary but let's only focus on the first and the last two lines.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In 1st line

&lt;code&gt;diff --git a/README.MD b/README.md&lt;/code&gt;

where

&lt;code&gt;a/README.md&lt;/code&gt;

and

&lt;code&gt;b/README.md&lt;/code&gt;

are the versions of the file in which changes are made

&lt;code&gt;a/README.md&lt;/code&gt;

is the version of &lt;strong&gt;README. MD&lt;/strong&gt; file present in your &lt;strong&gt;working directory&lt;/strong&gt; and

&lt;code&gt;b/README.md&lt;/code&gt;

is the version of &lt;strong&gt;README. MD&lt;/strong&gt; file present in your &lt;strong&gt;Local repository&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;In the last two lines starting with

&lt;code&gt;+&lt;/code&gt;

and

&lt;code&gt;-&lt;/code&gt;

. Which usually means

&lt;code&gt;+&lt;/code&gt;

line is added

&lt;code&gt;-&lt;/code&gt;

line is removed but as we are comparing

&lt;code&gt;a/README.md&lt;/code&gt;

(working directory version) with

&lt;code&gt;b/README.md&lt;/code&gt;

(local directory version).  Now it's showing we have removed

&lt;code&gt;-# git-test -This I have made this repository to show the implementation of git diff method.&lt;/code&gt;

lines because they are present in the local repository but not in the working repository. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;PS&lt;/strong&gt;: &lt;code&gt;git diff&lt;/code&gt; is a multi-use Git command that when executed runs a diff function on Git data sources. These data sources can be commits, branches, files, and more. for complete understanding check this &lt;a href="https://www.atlassian.com/git/tutorials/saving-changes/git-diff"&gt;Guide.&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Update Remote:
&lt;/h2&gt;

&lt;p&gt;After adding and reviewing changes now it's time to submit your code back to your own repository(which is a copy of the original repository).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git push &amp;lt;remote name&amp;gt; &amp;lt;branch name&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Make a pull request
&lt;/h2&gt;

&lt;p&gt;After Creating changes to your remote repository now you need to create a pull request to be able to merge your code with the source repository where the maintainer/owner will merge your code with the original repository after reviewing it.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--WrTBEuZt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://imgur.com/iwyA7og.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--WrTBEuZt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://imgur.com/iwyA7og.png" alt="" width="755" height="191"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--VqQPdSW9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://imgur.com/wg8YAPq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--VqQPdSW9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://imgur.com/wg8YAPq.png" alt="" width="880" height="180"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Congratulation🥳:
&lt;/h2&gt;

&lt;p&gt;Congratulations, if you read the complete article and stick till the end. For practice used my &lt;a href="https://github.com/shahabbukhari/git-test"&gt;&lt;strong&gt;git-test&lt;/strong&gt;&lt;/a&gt; repository. fork, clone, make changes, push and create a pull request&lt;br&gt;
&lt;a href="https://i.giphy.com/media/iFgD1JXOY4086dq6cl/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/iFgD1JXOY4086dq6cl/giphy.gif" alt="celebrate" width="480" height="270"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Thanks for reading.
&lt;/h2&gt;

&lt;p&gt;Feel free to connect with me on &lt;a href="https://www.linkedin.com/in/syed-shahab-shah-bukhari-33169a183/"&gt;LinkedIn&lt;/a&gt;. If you wanted to check what I am working follow me on &lt;a href="https://github.com/shahabbukhari"&gt;Github&lt;/a&gt; and Follow my &lt;a href="https://dev.to/shahabbukhari"&gt;blog&lt;/a&gt; for more beginner's centric content(because I am not an expert).&lt;/p&gt;

</description>
      <category>github</category>
      <category>git</category>
      <category>operations</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Git Simplified: A Beginner guide to git and GitHub</title>
      <dc:creator>shahabbukhari</dc:creator>
      <pubDate>Mon, 20 Sep 2021 11:16:31 +0000</pubDate>
      <link>https://dev.to/shahabbukhari/git-simplified-a-beginner-guide-to-git-and-github-19o8</link>
      <guid>https://dev.to/shahabbukhari/git-simplified-a-beginner-guide-to-git-and-github-19o8</guid>
      <description>&lt;h2&gt;
  
  
  What git actually is?
&lt;/h2&gt;

&lt;p&gt;Git is the most used tool for tracking changes in any set of files, usually used for coordinating work among programmers collaboratively developing source code during software development. &lt;/p&gt;

&lt;h2&gt;
  
  
  Umm... But why do i need Git.?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--umuU3Gau--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://media.giphy.com/media/IejsMRUQLlphOfMmI1/giphy.gif%3Fcid%3Decf05e47pefj9zdc2k22qrt4yimntpbsw8znbg3eox96vbl5%26rid%3Dgiphy.gif%26ct%3Dg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--umuU3Gau--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://media.giphy.com/media/IejsMRUQLlphOfMmI1/giphy.gif%3Fcid%3Decf05e47pefj9zdc2k22qrt4yimntpbsw8znbg3eox96vbl5%26rid%3Dgiphy.gif%26ct%3Dg" alt="confused" width="480" height="360"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  So here is why
&lt;/h3&gt;

&lt;p&gt;When you are working in a team. you have different task every person is responsible for different task but everyone's work is dependent on each others work. &lt;br&gt;
You have to send files to each-other when ever you changed something you have to send it back to back and maintaining those files becomes a tedious task.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0xZY_YNo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://imgur.com/J1g0v9v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0xZY_YNo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://imgur.com/J1g0v9v.png" alt="folder list" width="717" height="348"&gt;&lt;/a&gt;&lt;br&gt;
Here comes the &lt;strong&gt;git&lt;/strong&gt;. Git tracks changes in your files and save different version of files. if you make any crap in your current version you can easily go back to your previous stable version. &lt;br&gt;
if you are working in a team and something goes wrong. It will help you to identity &lt;strong&gt;who&lt;/strong&gt; made the change and &lt;strong&gt;where&lt;/strong&gt; he/she made the change and &lt;strong&gt;when&lt;/strong&gt; they made a change&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--V1VmokZ9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://docs.microsoft.com/en-us/devops/_img/history-abcd-cool-new-feature-1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--V1VmokZ9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://docs.microsoft.com/en-us/devops/_img/history-abcd-cool-new-feature-1.png" alt="git history" width="355" height="159"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Prerequisites:
&lt;/h2&gt;

&lt;p&gt;Before we start working on git there are some prerequisites:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;[x] Install Git: 

&lt;ul&gt;
&lt;li&gt;  Mac Users click &lt;a href="http://git-scm.com/download/mac"&gt;here&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;  Windows Users click &lt;a href="https://gitforwindows.org/"&gt;here&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;  Linux Users click &lt;a href="http://git-scm.com/book/en/v2/Getting-Started-Installing-Git"&gt;here&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;  [x] Github Account:

&lt;ul&gt;
&lt;li&gt;If you don't already have one, you can make &lt;a href="https://github.com/signup?ref_cta=Sign+up&amp;amp;ref_loc=header+logged+out&amp;amp;ref_page=%2F&amp;amp;source=header-home"&gt;one&lt;/a&gt; &lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Git Basics:
&lt;/h2&gt;

&lt;p&gt;Before we start working with there are some things you need to know.&lt;/p&gt;
&lt;h2&gt;
  
  
  Git repository:
&lt;/h2&gt;

&lt;p&gt;Git repository aka repo is like a normal folder but with some Super powers. It saves every change you make in it.&lt;/p&gt;
&lt;h2&gt;
  
  
  Git Workflow:
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--hyosgJnp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/practicaldev/image/fetch/s--M_fHUEqA--/c_limit%252Cf_auto%252Cfl_progressive%252Cq_auto%252Cw_880/https://thepracticaldev.s3.amazonaws.com/i/128hsgntnsu9bww0y8sz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hyosgJnp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/practicaldev/image/fetch/s--M_fHUEqA--/c_limit%252Cf_auto%252Cfl_progressive%252Cq_auto%252Cw_880/https://thepracticaldev.s3.amazonaws.com/i/128hsgntnsu9bww0y8sz.png" alt="Git workflow" width="880" height="625"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If your file is in current working directory(folder). it can be in three states:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Working Directory:&lt;/strong&gt; Working directory is the area where you are currently working. where your files resides. it is also called &lt;strong&gt;Working Tree&lt;/strong&gt; or &lt;strong&gt;Untracked Area&lt;/strong&gt;. every change you make here is not tracked by git.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Staging Area:&lt;/strong&gt; Staging area is when git starts tracking and saving changes occur in file. after saving changes when you make another change in the file git will know where you made a change. with

&lt;code&gt;git add filename&lt;/code&gt;

you can move file from &lt;strong&gt;working directory&lt;/strong&gt;(Untracked Stage) to &lt;strong&gt;Staging Area&lt;/strong&gt;(Tracked Stage).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Local Repo:&lt;/strong&gt; local repository is the place where you save your changes before sending them on remote repository. you can save changes from &lt;strong&gt;staging area&lt;/strong&gt; with

&lt;code&gt;git commit -m "message" filename)"&lt;/code&gt;

.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Remote Repo:&lt;/strong&gt; Remote repository is a place where you &lt;strong&gt;Push&lt;/strong&gt; your code or &lt;strong&gt;Pull&lt;/strong&gt; to update the local version of a repository from a remote repository. its usually &lt;strong&gt;Github&lt;/strong&gt; or any other hosting service for version control.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Git Basic Commands:
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--tJi2dugu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://media.giphy.com/media/m5wMKLifjawJQ9FaMC/giphy.gif%3Fcid%3Decf05e47hlzebxk5qr5ha8j7s7v63bz7064rxwejll2efuyw%26rid%3Dgiphy.gif%26ct%3Dg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--tJi2dugu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://media.giphy.com/media/m5wMKLifjawJQ9FaMC/giphy.gif%3Fcid%3Decf05e47hlzebxk5qr5ha8j7s7v63bz7064rxwejll2efuyw%26rid%3Dgiphy.gif%26ct%3Dg" alt="start" width="480" height="270"&gt;&lt;/a&gt;&lt;br&gt;
Now, Its time to get our hands dirty on git commands. &lt;strong&gt;Lets Git&lt;/strong&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  Initilizing Local Repository:
&lt;/h2&gt;

&lt;p&gt;Before you can use any &lt;strong&gt;Git&lt;/strong&gt; super powers first you need a repository(Super Folder). Either you can connect your existing project folder to your git repository. or you can clone one from github.&lt;/p&gt;
&lt;h3&gt;
  
  
  Clone a repository from github:
&lt;/h3&gt;

&lt;p&gt;First create a repository on github with &lt;strong&gt;new&lt;/strong&gt; button from &lt;strong&gt;repository&lt;/strong&gt; section.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--cLoYnvKZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://imgur.com/7STmiPI.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--cLoYnvKZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://imgur.com/7STmiPI.png" alt="git repo" width="880" height="112"&gt;&lt;/a&gt;&lt;br&gt;
Now add required details &lt;strong&gt;Repository name&lt;/strong&gt;, &lt;strong&gt;Description&lt;/strong&gt; and so on. then create a repository.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8r0h0KIy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://imgur.com/GPaykll.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8r0h0KIy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://imgur.com/GPaykll.png" alt="add details" width="596" height="472"&gt;&lt;/a&gt;&lt;br&gt;
After creating a repository copy &lt;strong&gt;HTTP&lt;/strong&gt; or &lt;strong&gt;SSH&lt;/strong&gt; Address.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8CvNHiYR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://imgur.com/qOyiOpt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8CvNHiYR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://imgur.com/qOyiOpt.png" alt="Repo Adress" width="880" height="109"&gt;&lt;/a&gt;&lt;br&gt;
Now clone your repository into your local machine.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git clone address_of_your_repository
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Connect Existing project with remote repo:
&lt;/h3&gt;

&lt;p&gt;If you want to connect your existing project to your git repository you can use this.&lt;br&gt;
First initialize a git repository in that folder.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now connect your existing repository with remote repository.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git remote add remote_name address_of_your_repository
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At &lt;strong&gt;remote_name&lt;/strong&gt; you can specify any name you want to specify for your remote repository. usually it's origin.&lt;/p&gt;

&lt;h3&gt;
  
  
  Add Changes to stage:
&lt;/h3&gt;

&lt;p&gt;To add changes from your working directory to staging area.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git add /path/to/file
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can add all changes at one(if you are lazy like me).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git add &lt;span class="nb"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Commit changes
&lt;/h3&gt;

&lt;p&gt;Now commit(save) the changes you added to your &lt;strong&gt;staging area&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git commit &lt;span class="nt"&gt;-m&lt;/span&gt; &lt;span class="s2"&gt;"Commit Message"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The message in &lt;strong&gt;" "&lt;/strong&gt; is given so that the other users can read the message and see what changes you made.&lt;/p&gt;

&lt;h3&gt;
  
  
  Check Status of Files:
&lt;/h3&gt;

&lt;p&gt;You can check the status of the files in working directory and the staging area. which files are in staging area and which files or not tracked yet.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git status
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4wmu13SX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.webfx.com/blog/images/assets/cdn.sixrevisions.com/0363-06_git_status02.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4wmu13SX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.webfx.com/blog/images/assets/cdn.sixrevisions.com/0363-06_git_status02.png" alt="git status" width="550" height="280"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Same as &lt;code&gt;git status&lt;/code&gt; you can also check how many commits you have made in your &lt;strong&gt;git local repository&lt;/strong&gt;(all the commits made to a repository).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git log
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fnHFDqcD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://imgur.com/dG43tAx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fnHFDqcD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://imgur.com/dG43tAx.png" alt="git log" width="713" height="401"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Push Code:
&lt;/h3&gt;

&lt;p&gt;To upload your local repository content to a remote repository(Github). we use.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git push remote_name branch_name 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;remote_name:&lt;/strong&gt; usually its &lt;strong&gt;origin&lt;/strong&gt; but in start if you initialize your existing project to git repository you may have added a remote_ name in &lt;code&gt;git remote add remote_name address_of_your_repository&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;branch_name:&lt;/strong&gt; its a branch where you want to submit your code. usually its &lt;strong&gt;master&lt;/strong&gt; or &lt;strong&gt;main&lt;/strong&gt;. you can check your branches with

&lt;code&gt;git branch&lt;/code&gt;

it will list all the branches. the one with * before name is your your current branch.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; git maintain history of your project when your &lt;strong&gt;local repo&lt;/strong&gt; history doesn't match with &lt;strong&gt;remote repo&lt;/strong&gt; you cannot push your code. It may happen because of some files changes made by you/teammate in remote repository which are not present in local repository.&lt;br&gt;
So when you are working in team/collaborative project you may want to &lt;strong&gt;pull&lt;/strong&gt; changes in to your local working directory.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git pull remote_name branch_name
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;git pull&lt;/strong&gt; update the local version of a repository from a remote repository. &lt;br&gt;
Now Push your changes to remote repository.&lt;/p&gt;

&lt;h4&gt;
  
  
  Congratulations🥳
&lt;/h4&gt;

&lt;p&gt;Congratulations, you made your first push to your remote repository.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qHINOXOn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://media.giphy.com/media/IwAZ6dvvvaTtdI8SD5/giphy-downsized.gif%3Fcid%3Decf05e477ppiywu1k7m3ztcq3516i2i0p4xicn117k1xnpy3%26rid%3Dgiphy-downsized.gif%26ct%3Dg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qHINOXOn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://media.giphy.com/media/IwAZ6dvvvaTtdI8SD5/giphy-downsized.gif%3Fcid%3Decf05e477ppiywu1k7m3ztcq3516i2i0p4xicn117k1xnpy3%26rid%3Dgiphy-downsized.gif%26ct%3Dg" alt="celebrate" width="480" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion:
&lt;/h2&gt;

&lt;p&gt;In this article we have learned what git is, why we need git, some basic git terminologies and some basic git commands.&lt;br&gt;
Stay tuned for the next article on how to work collaboratively on git in a team.&lt;/p&gt;

&lt;h2&gt;
  
  
  Thanks for reading.
&lt;/h2&gt;

&lt;p&gt;Feel free to connect with me on &lt;a href="https://www.linkedin.com/in/syed-shahab-shah-bukhari-33169a183/"&gt;LinkedIn&lt;/a&gt;. If you wanted to check what i am working follow me on &lt;a href="https://github.com/shahabbukhari"&gt;Github&lt;/a&gt; and Follow my &lt;a href="https://dev.to/shahabbukhari"&gt;blog&lt;/a&gt; for more beginner's centric content(bcz i am not an expert).&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--c3LhI0co--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://media.giphy.com/media/BYoRqTmcgzHcL9TCy1/giphy.gif%3Fcid%3D790b7611ebb4990f1e13964f2eaa5b566dd35ea7930636a7%26rid%3Dgiphy.gif%26ct%3Dg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--c3LhI0co--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://media.giphy.com/media/BYoRqTmcgzHcL9TCy1/giphy.gif%3Fcid%3D790b7611ebb4990f1e13964f2eaa5b566dd35ea7930636a7%26rid%3Dgiphy.gif%26ct%3Dg" alt="Thanks" width="358" height="360"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>github</category>
      <category>git</category>
      <category>programming</category>
      <category>opensource</category>
    </item>
  </channel>
</rss>
