<?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: Aditya Tiwari</title>
    <description>The latest articles on DEV Community by Aditya Tiwari (@tiwariaditya).</description>
    <link>https://dev.to/tiwariaditya</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%2F399089%2F622b9e24-b709-4e0a-914a-2e780b99d708.png</url>
      <title>DEV Community: Aditya Tiwari</title>
      <link>https://dev.to/tiwariaditya</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tiwariaditya"/>
    <language>en</language>
    <item>
      <title>Scope &amp; Closure Ch. 1&amp;2 Summary(YDKJSY)</title>
      <dc:creator>Aditya Tiwari</dc:creator>
      <pubDate>Fri, 10 Sep 2021 13:53:42 +0000</pubDate>
      <link>https://dev.to/tiwariaditya/scope-closure-ch-1-2-summary-ydkjsy-1pdo</link>
      <guid>https://dev.to/tiwariaditya/scope-closure-ch-1-2-summary-ydkjsy-1pdo</guid>
      <description>&lt;p&gt;This blog post is a summary of what I could understand from the first 2 chapters of this book. &lt;/p&gt;

&lt;h1&gt;
  
  
  Chapter 1: What's Scope?
&lt;/h1&gt;

&lt;p&gt;Scope refers to a part of the program where variables/data declared in that part of the program are accessible to the program based on where they were declared. Confusing? Let's try one more time. A variable in any part of a program can be accessed by the program in other parts based on where the variable was declared/exists. This is where the term &lt;code&gt;scope&lt;/code&gt; comes in to simplify things and make our understanding better by actually having some rules that help to predict/understand and access variables/functions easier.&lt;/p&gt;

&lt;h2&gt;
  
  
  Compiled vs Interpreted
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;What does compiled or interpreted mean? So computers don't understand human language. They work on 1's and 0's. So in the initial days, people wrote code in assembly language that was converted to machine code by processors. But it was hard you didn't get a lot of flexibility with those languages. On top of that, it was harder to understand and learn. That's where languages that were close to English started coming in ex. Fortran, COBOL.&lt;/li&gt;
&lt;li&gt;But we still had one issue ⇒ Computers don't understand that. Computers need instruction to complete tasks.&lt;/li&gt;
&lt;li&gt;That's where this process comes in that converts a program that's written in a high-level language to instructions that the computer would understand.&lt;/li&gt;
&lt;li&gt;This process has different models like &lt;em&gt;compiled&lt;/em&gt; and &lt;em&gt;interpreted&lt;/em&gt;. How do they differ? In code compilation, the compiler compiles the whole file in one pass and then generates a new file that could be used to run the program later. In the interpreted model, your program still compiles the code in machine instruction but instead of doing that at once, it does this line by line.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Is JS interpreted language?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;So most of the time, JS is considered an &lt;em&gt;interpreted&lt;/em&gt; language but Kyle has written and gave proof that how JS isn't &lt;em&gt;interpreted&lt;/em&gt; but &lt;em&gt;compiled&lt;/em&gt; in the first book.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;What does compiling code mean?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Why does it even matter if JS is compiled or interpreted? Since we're discussing scope, the scope is defined during this phase only.&lt;/li&gt;
&lt;li&gt;What does happen during compilation? So basically there are 3 steps taken during compiling any code.

&lt;ol&gt;
&lt;li&gt;Tokenizing/Lexing: This step refers to tagging reserved keywords of language. Breaking whole program into chunks that language understands.&lt;/li&gt;
&lt;li&gt;Parsing: Taking streams of tokens and turning them into a tree of several elements. This is called as &lt;code&gt;Abstract Syntax Tree(AST)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Code Generation: Converting the received AST to an executable code by machine.&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Two Phases
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Compilation Phase&lt;/li&gt;
&lt;li&gt;Execution Phase&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;Kyle said these 2 phases can be actually observed through JS programs and are not just facts in theory.&lt;/li&gt;
&lt;li&gt;We can observe this pattern if we look and notice the following things: &lt;code&gt;syntax error&lt;/code&gt;, &lt;code&gt;early errors&lt;/code&gt;, &lt;code&gt;hoisting&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Compiler Speaks
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The following program has an array of addresses that has two property city and country. We see a function &lt;code&gt;getCountry&lt;/code&gt; that receives a parameter named city and based on city name, it returns name of associated country inside object.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;addresses&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="na"&gt;city&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;SF&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;country&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;US&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="na"&gt;city&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;MUM&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;country&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;IND&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="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;getCountry&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="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;address&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;addresses&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;address&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;city&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;city&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;address&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;country&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="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;country&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;getCountry&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;SF&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;country&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Every variable/identifier in any program act as one of these 2 roles: &lt;code&gt;target&lt;/code&gt; and &lt;code&gt;source&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  What's the target/source? How does someone identify them?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Variables that are being assigned a value are a target in our program and rest of the variable/identifiers are sources.&lt;/li&gt;
&lt;li&gt;In above code, &lt;code&gt;addresses&lt;/code&gt;, &lt;code&gt;city&lt;/code&gt;parameter of getCountry function, &lt;code&gt;address&lt;/code&gt;in for loop block and &lt;code&gt;country&lt;/code&gt; are targets of this program. Since they were being assigned value. But we left one more target to mentions above.&lt;/li&gt;
&lt;li&gt;That's &lt;code&gt;function getCountry(city)&lt;/code&gt;. Yes, function declarations are subtle target reference that exists in JS codes.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;Scope is defined during compilation phase, so you can't really change it during runtime. But JS also has ways do this through &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;eval(...)&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;badIdea&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nb"&gt;eval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;var a = 2;&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="nx"&gt;badIdea&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;with()&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;info&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;John&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="kd"&gt;with&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;info&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;name&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;age&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;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So following way, you could still change scope during runtime but Kyle suggested avoiding this at all costs and they're not available in strict mode anyways.&lt;/p&gt;

&lt;h1&gt;
  
  
  Chapter 2: Illustrating Lexical Scope
&lt;/h1&gt;

&lt;p&gt;Let's have a look at the code snippet that we used in the last section.&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;const&lt;/span&gt; &lt;span class="nx"&gt;addresses&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="na"&gt;city&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;SF&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;country&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;US&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="na"&gt;city&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;MUM&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;country&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;IND&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="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;getCountry&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="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;address&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;addresses&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;address&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;city&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;city&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;address&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;country&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="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;country&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;getCountry&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;SF&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;country&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now Kyle came up with the following metaphor/mental model to understand scope through&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Marbles, Bucket and Bubbles&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now I am gonna explain/write what I understood through his metaphor and explanations. For an accurate mental model, I suggest reading &lt;a href="%5Bhttps://github.com/getify/You-Dont-Know-JS/blob/2nd-ed/scope-closures/ch2.md#marbles-and-buckets-and-bubbles-oh-my%5D(https://github.com/getify/You-Dont-Know-JS/blob/2nd-ed/scope-closures/ch2.md#marbles-and-buckets-and-bubbles-oh-my)"&gt;this&lt;/a&gt; section.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In the code above, there are 3 scopes that we can observe. The outer scope ie Global Scope, the scope of the function, and since JS now blocks also create new scope, we can see the scope created by for loop.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Through Kyle's metaphor,&lt;/p&gt;

&lt;p&gt;⇒ a scope is represented by a colored bubble&lt;/p&gt;

&lt;p&gt;⇒ each scope has their own scope bucket&lt;/p&gt;

&lt;p&gt;⇒ a variable/identifier represents marble and belongs to a scope bucket depending on the bubble it resides.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So  we have 3 different scopes we would say&lt;/p&gt;

&lt;p&gt;BUBBLE 1 - The outermost scope holding marbles &lt;code&gt;addresses&lt;/code&gt;, &lt;code&gt;country&lt;/code&gt; and &lt;code&gt;getCountry&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;BUBBLE 2 - The scope of function getCountry holding marble &lt;code&gt;city&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;BUBBLE 3  - The scope of for-loop holding marble &lt;code&gt;address&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  How does scoping work in their bubble?
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;A scope can only access its outer scope and can not access scopes that are nested inside them.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;⇒ So expressions inside BUBBLE1 &lt;code&gt;can access&lt;/code&gt; marbles of BUBBLE1, &lt;code&gt;not&lt;/code&gt;  BUBBLE2, &lt;code&gt;not&lt;/code&gt; BUBBLE3. &lt;/p&gt;

&lt;p&gt;⇒ An expression inside BUBBLE2 &lt;code&gt;can access&lt;/code&gt; marbles of BUBBLE2, &lt;code&gt;can access&lt;/code&gt; marbles of BUBBLE1, but &lt;code&gt;not&lt;/code&gt; BUBBLE3.&lt;/p&gt;

&lt;p&gt;⇒ An expression inside BUBBLE3 &lt;code&gt;can access&lt;/code&gt; marbles of BUBBLE3, &lt;code&gt;can access&lt;/code&gt; marbles of BUBBLE2, and &lt;code&gt;can access&lt;/code&gt; marble of BUBBLE1.&lt;/p&gt;

&lt;h2&gt;
  
  
  A conversation among friends
&lt;/h2&gt;

&lt;p&gt;In this section, Kyle wrote about how these variables are put into respective bubble and scope buckets during compilation and how does the lookup happen for the marbles &lt;em&gt;aka&lt;/em&gt; variables/identifiers during code execution based on bubbles.&lt;/p&gt;

&lt;p&gt;Whenever JS engines would start processing code, it would happen in 2 phases&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Compilation&lt;/li&gt;
&lt;li&gt;Code execution&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;what does happen in the &lt;em&gt;compilation&lt;/em&gt; phase?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;compiler starts compiling the code and it takes help of scope manager to create declared variables&lt;/li&gt;
&lt;li&gt;compiler also asks scope manager to create functions declaration and create a separate scope for that function with its own scope bucket and scope manager ie &lt;em&gt;function scope manager&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;compiler does this for all of target references it finds and asks scope manager, maybe global scope manager or maybe functions' scope manager or maybe a new blocks' scope manager depending on where the control of the program is in right now, to create that space in memory&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;what does happen in the execution phase?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Now in this phase engine asks respective scope managers if the target reference it found exists in scope from the compilation phase. If scope manager says, yes it exists in current scope then engine assigns undefined to it so that it's ready to use when program actually starts execution&lt;/li&gt;
&lt;li&gt;If the current scope manager doesn't have that target reference then it asks engine to go in outer scopes and ask the respective scope manager&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Nested Scopes
&lt;/h2&gt;

&lt;p&gt;We saw if the current scope doesn't have a variable/identifier then engine goes to next outer scope and asks respective scope manager. This stops once the engine reaches global scope. If engine doesn't find the variable in global scope as well then it results in an error. These errors are handled differently based on if they're target/source. Usually, a reference error is thrown.&lt;/p&gt;

&lt;h3&gt;
  
  
  accidental globals
&lt;/h3&gt;

&lt;p&gt;In non-strict mode, when a program tries to assign a target reference a value and if value was never declared either in current scope or in next outer scope then scope manager(global) goes ahead and creates a new variable but this look should've resulted in failure.&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;function&lt;/span&gt; &lt;span class="nx"&gt;getStudentName&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// assignment to an undeclared variable :(&lt;/span&gt;
    &lt;span class="nx"&gt;nextStudent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Suzy&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="nx"&gt;getStudentName&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;nextStudent&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// "Suzy" -- oops, an accidental-global variable!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  One more metaphor
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--m9Dp2Tu7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://raw.githubusercontent.com/getify/You-Dont-Know-JS/2nd-ed/scope-closures/images/fig3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--m9Dp2Tu7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://raw.githubusercontent.com/getify/You-Dont-Know-JS/2nd-ed/scope-closures/images/fig3.png" alt="https://raw.githubusercontent.com/getify/You-Dont-Know-JS/2nd-ed/scope-closures/images/fig3.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Kyle came up with one more metaphor is an office building. So if you're trying to resolve a target/source reference so you first start by searching the first floor and when you don't find that you proceed to search on the next floor and you do this until you reach last floor in building and you have no more floor to go.&lt;/p&gt;

</description>
      <category>javascript</category>
    </item>
    <item>
      <title>What's Critical Rendering Path?</title>
      <dc:creator>Aditya Tiwari</dc:creator>
      <pubDate>Sat, 03 Jul 2021 13:50:01 +0000</pubDate>
      <link>https://dev.to/tiwariaditya/what-s-critical-rendering-path-3p7e</link>
      <guid>https://dev.to/tiwariaditya/what-s-critical-rendering-path-3p7e</guid>
      <description>&lt;p&gt;Whenever we talk about web performance, we come across things like async/defer, perceived performance, and critical path rendering. In this blog, we're going to discuss in length about Critical Rendering Path and how does it affect web performance.  &lt;/p&gt;

&lt;p&gt;We can define CRP in one line as &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The steps taken by the browser to convert HTML, CSS, and JavaScript into pixels, that can be painted on the browser, are called Critical Rendering Path(CRP).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The whole process involves several steps and processing of code. The following flow would give you an idea of it - &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Whenever you hit some page on internet, browser goes to server and requests the page it needs.&lt;/li&gt;
&lt;li&gt;Server replies with the data on the network. The data comes in form of &lt;em&gt;bytes&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;Once the data has come back CRP starts executing steps to process HTML files&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  🛠CRP steps -
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Document Object Model(DOM) Construction -
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Browser reads the HTML, CSS, and JS files and starts converting data to HTML markup. It uses a defined &lt;code&gt;unicode&lt;/code&gt; type with an HTML page to convert data into characters.&lt;/li&gt;
&lt;li&gt;After browser is done converting characters, it starts tokenizing the HTML page. Tokenizing is done to identify different tags and form nodes based on them. With tokenizing browser also converts the HTML element data into &lt;code&gt;Objects&lt;/code&gt; because it needs to store information such as &lt;code&gt;parentNode&lt;/code&gt;, &lt;code&gt;childNodes&lt;/code&gt;, events attached to that respective element etcetera.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--iAd7CymT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1625317169259/kEOtlZoQeF.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--iAd7CymT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1625317169259/kEOtlZoQeF.png" alt="DOM nodes.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Once tagging is done and browser knows HTML elements then browser starts constructing nodes. Nodes store information about HTML elements.&lt;/li&gt;
&lt;li&gt;After constructing nodes, browser starts constructing DOM and establishes a relationship between nodes as a parent, child, and siblings.&lt;/li&gt;
&lt;li&gt;While DOM construction, if browser encounters external resources like JS, images then it is blocking request. Browser waits for request to resolve and then restarts DOM construction.&lt;/li&gt;
&lt;li&gt;So depending on external resource fetching, DOM construction time &lt;em&gt;varies&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;We can avoid blocking resource by fetching non-critical data using async/defer keywords.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  2. CSS Object Model -
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Browser performs CSSOM construction after DOM is ready but CSSOM construction is render-blocking because browser waits for different CSS files to arrive.&lt;/li&gt;
&lt;li&gt;This behaviour isn't strange. It exists for a reason.&lt;/li&gt;
&lt;li&gt;CSS cascades down from parent elements. It means, &lt;em&gt;say for example&lt;/em&gt;, styles given to body tag would go all the way down in DOM tree. But we might override the cascading styles from parents in child. So browser waits until it receives whole CSS and then constructs the CSSOM.&lt;/li&gt;
&lt;li&gt;With custom styles given to any HTML page, there are set of pre-defined style that exists in browser. That is the reason, even if you write plain HTML files without styles. You would observe some basic styling done by browser. So CSSOM is constructed using both custom and pre-defined styles by browser.&lt;/li&gt;
&lt;li&gt;Usually more specific rules increase the amount of work browser has to do.&lt;/li&gt;
&lt;li&gt;For example, .inner {} selector would take less time than .outer .inner { } selector. Because once browser finds .inner { }, it has to go and find it's parent .outer { } also.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  3. Render Tree -
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Browser has DOM and CSS trees at this point. So browser knows what do we want to put on screen and how to style it but independently they don't mean anything.&lt;/li&gt;
&lt;li&gt;So to construct render tree, browser would have to visit each node in DOM and find each nodes respective styling from CSSOM and finally construct a combination of both in a tree known as render tree.&lt;/li&gt;
&lt;li&gt;So it combines DOM and CSSOM but it doesn't put every node from DOM in here.&lt;/li&gt;
&lt;li&gt;Render Tree stores HTML elements/node that needs to be in view. So it doesn't have HTML elements like head, meta, script etcetera.&lt;/li&gt;
&lt;li&gt;It also doesn't store elements that have property &lt;code&gt;display: none&lt;/code&gt; or any of its descendants. Because render tree represents elements that are going to be painted on screen. So it omits any element that would not be part of our layout. We would talk in length about layout in the next step.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  4. Layout -
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Now browser has a render tree, that stores elements and their respective styles to be put in the browser screen. But during whole phase, browser didn't compute any position or size-related property. So how does the browser know what to put where? It needs some information so that it constructs the layout of HTML page.&lt;/li&gt;
&lt;li&gt;That part is done here because based on &lt;code&gt;viewport&lt;/code&gt; of device, these properties vary. This process is called &lt;code&gt;layout&lt;/code&gt; or &lt;code&gt;reflow&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Time taken by browser to do this layout depends directly on size of DOM tree because it has to perform render tree step again.&lt;/li&gt;
&lt;li&gt;What causes layout/reflow in browser? So window resizing, device rotation, scroll etcetera are responsible to make browser do layout again. Since on all these events, size/position of elements would definitely change.&lt;/li&gt;
&lt;li&gt;In this step, every relative unit, i.e. %, rem etcetera, is converted to specific pixels.&lt;/li&gt;
&lt;li&gt;This &lt;a href="https://gist.github.com/paulirish/5d52fb081b3570c81e3a#:~:text=So%2C%20if%20you%20make%20dom,ll%20get%20a%20sync%20layout"&gt;GitHub gist&lt;/a&gt; shows what forces layout in browsers.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  5. Paint -
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;With layout done, only part left is putting elements on screen in form of pixels.&lt;/li&gt;
&lt;li&gt;Browser uses render tree to do this painting. Time taken by this step depends on size of DOM and amount of work required by browser to do for styling, layout, render tree construction.&lt;/li&gt;
&lt;/ul&gt;




&lt;ul&gt;
&lt;li&gt;We saw steps involved in converting data from &lt;em&gt;bytes&lt;/em&gt; to &lt;em&gt;pixels&lt;/em&gt; that the browser performs. That means the time taken by CRP is the initial loading time of our web app.&lt;/li&gt;
&lt;li&gt;If we want to reduce the loading time of our web app then we need to optimize our CRP. Optimizing CRP lies in above steps. In order to optimize your CRP, you need to optimize each step and reduce the amount of time browser spends on each of them.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  🤔How do we optimize CRP?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Basically you need to reduce the time taken by each steps to optimize overall CRP but how would you do that?&lt;/li&gt;
&lt;li&gt;If we go back for a refresher then we know that browser does the DOM construction and CSSOM, right?&lt;/li&gt;
&lt;li&gt;Can we do something here? Yes. Let's discuss the solutions now.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;To optimize DOM construction -&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;As we already saw, DOM is render-block. It is render blocking because if it encounters a link or a script tag, browser stops DOM construction waits and after the link is fetched or JS engine has completed running JS code in script tag, browser starts where it left the construction.&lt;/li&gt;
&lt;li&gt;Browser does this because, when it encounters a script tag, it doesn't know what changes that script would do after completion. This is the same case even if you don't write JS code directly in script tag but write it in a different file and link via a script tag. Browser would still behave the same.&lt;/li&gt;
&lt;li&gt;So in order to load app as soon as possible, we need to decide what our critical resources are and what aren't?&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;You can do this by attaching a &lt;code&gt;async&lt;/code&gt; attribute on your script tag like this&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;script&lt;/span&gt; &lt;span class="na"&gt;src&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"index.js"&lt;/span&gt; &lt;span class="na"&gt;async&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;script&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When browser encounters &lt;code&gt;async&lt;/code&gt; tag, it understands that this is not critical resource for page, it doesn't stop and keeps the DOM construction going to next part of file.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;&lt;p&gt;How can we optimize the CSSOM construction step?&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  🧐CSS perf optimizations -
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;As we saw, DOM and CSSOM construction are render-blocking and it affects CRP. We can't do anything for DOM cause to start painting pixels on browser we need elements but can we optimize CSSOM construction.&lt;/li&gt;
&lt;li&gt;If we think our styles are composed of different things like we would have different set of styles for mobile devices and desktop devices.&lt;/li&gt;
&lt;li&gt;At one point, we would use just one of them. So we can define and ask browser what to load and move to render tree(discussed in step 3) step instead of waiting for whole styles to arrive and load.&lt;/li&gt;
&lt;li&gt;We can do this in several ways. Some of it is defining &lt;code&gt;media types&lt;/code&gt; and &lt;code&gt;media queries&lt;/code&gt; when we link our CSS files.&lt;/li&gt;
&lt;li&gt;If you link CSS files like this
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;link&lt;/span&gt; &lt;span class="na"&gt;rel&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"stylesheet"&lt;/span&gt; &lt;span class="na"&gt;href&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"index.css"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;then it's CSS parse blocking.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If you know some parts of CSS would only be applied if the page loads on Mobile Devies or smaller screens. So you could defined them like this
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;link&lt;/span&gt; &lt;span class="na"&gt;rel&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"stylesheet"&lt;/span&gt; &lt;span class="na"&gt;href&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"mobile.css"&lt;/span&gt; &lt;span class="na"&gt;media&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"screen and (max-width: 680px)"&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;link&lt;/span&gt; &lt;span class="na"&gt;rel&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"stylesheet"&lt;/span&gt; &lt;span class="na"&gt;href&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"portrait.css"&lt;/span&gt; &lt;span class="na"&gt;media&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"orientation:portrait"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;then this wouldn't be blocking since it only loads when screen size is under 680px.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In second link tag, this would only load when orientation would be portrait.&lt;/li&gt;
&lt;li&gt;Putting animations on GPU instead putting heavy computations on main thread&lt;/li&gt;
&lt;li&gt;Using property like &lt;code&gt;will-change&lt;/code&gt; to let browser know beforehand that this property would change in the future. If browser encounters this property then it does some optimizations even before the element actually changes.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  ⚡If we want to put optimizations in points then -
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Optimize your critical resources so that browser doesn't waste a lot of time fetching them.&lt;/li&gt;
&lt;li&gt;Load critical resources as early as possible. Don't make your browser wait for them.&lt;/li&gt;
&lt;li&gt;Understand what's important for first or initial load of your app and defer rest of resources by making them &lt;code&gt;async&lt;/code&gt; and loading them later.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;📚 Resources - &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://developers.google.com/web/fundamentals/performance/critical-rendering-path"&gt;Web Fundamentals&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/Performance/Critical_rendering_path"&gt;MDN Docs&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://medium.com/@luisvieira_gmr/understanding-the-critical-rendering-path-rendering-pages-in-1-second-735c6e45b47a"&gt;https://medium.com/@luisvieira_gmr/understanding-the-critical-rendering-path-rendering-pages-in-1-second-735c6e45b47a&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>browsers</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Are you using context right? </title>
      <dc:creator>Aditya Tiwari</dc:creator>
      <pubDate>Mon, 19 Apr 2021 11:31:44 +0000</pubDate>
      <link>https://dev.to/tiwariaditya/are-you-using-context-right-1hkp</link>
      <guid>https://dev.to/tiwariaditya/are-you-using-context-right-1hkp</guid>
      <description>&lt;p&gt;Did you ever think why does context exists in react? You might have seen people asking on the internet that when to use context or context+useReduer and when to use redux. So let's understand the use cases of context in react and how hooks have made life easier.&lt;/p&gt;

&lt;p&gt;React docs define Context as -&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Context provides a way to pass data through the component tree without having to pass props down manually at every level.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This means that context helps us in avoiding prop drilling. Wait but why should we avoid prop drilling? What is wrong with prop drilling?&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Os0I0YJ6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xwkml001aqetw4mpyxcr.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Os0I0YJ6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xwkml001aqetw4mpyxcr.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
You wouldn't understand why prop drilling is bad until you go through the pain of passing down a single value all the way from the top of the DOM tree to deep down in some component.&lt;/p&gt;

&lt;h2&gt;
  
  
  Let's see an example of prop drilling...
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// App.js
export default function App() {
  const [userName] = useState("Mr. Ferb");
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;Dashboard userName={userName} /&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

// Dashboard.js
export default function Dashboard({ userName }) {
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;Profile userName={userName} /&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

// Profile.js
export default function Profile({ userName }) {
  return &amp;lt;div&amp;gt;
    &amp;lt;h1&amp;gt;Welcome { userName } &amp;lt;/h1&amp;gt;
  &amp;lt;/div&amp;gt;;
}

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;In the above code, we're trying to pass the username of logged-in user from App component to Profile component. But profile component isn't enclosed inside app but it lies in Dashboard. Dashboard is enclosed in App. So to make this &lt;code&gt;userName&lt;/code&gt; available to &lt;code&gt;&amp;lt;Profile /&amp;gt;&lt;/code&gt; component, we would have to pass it as props to &lt;code&gt;&amp;lt;Dashboard /&amp;gt;&lt;/code&gt; first and then from &lt;code&gt;&amp;lt;Dashboard /&amp;gt;&lt;/code&gt; again to &lt;code&gt;&amp;lt;Profile /&amp;gt;&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Now this was a hypothetical scenario and we were passing just a single value but consider the real complex application where your state is needed in many parts of the DOM tree. You would reach a point in your app where it would become really hard for you to track down the value and bugs that are being passed down through props.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;So this is where Context comes in to save us. So in order to use context react gives us a hook called &lt;code&gt;useContext&lt;/code&gt;. (Context can also be used  with class-based components but we are going to cover the hooks part only.)&lt;/p&gt;

&lt;h2&gt;
  
  
  Using useContext
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;React gives us an API &lt;code&gt;createContext&lt;/code&gt;. This returns a Context object and our components can subscribe to this returned context object and consume those values that exist in this context.&lt;/li&gt;
&lt;li&gt;Now we would code the previous example where we tried accessing the &lt;code&gt;userName&lt;/code&gt; inside Profile without context.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// UsernameContext.js
import { createContext } from "react";
const UsernameContext = createContext();
export default UsernameContext;

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Above we used createContext API and made this UsernameContext that would hold the username for our app. (You can pass initial values right here in createContext as well).
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// index.js
import UsernameContext from "./UsernameContext";
const intialValues = { userName: "Mr. Ferb" };
ReactDOM.render(
  &amp;lt;StrictMode&amp;gt;
    &amp;lt;UsernameContext.Provider value={intialValues}&amp;gt;
      &amp;lt;App /&amp;gt;
    &amp;lt;/UsernameContext.Provider&amp;gt;
  &amp;lt;/StrictMode&amp;gt;,
  rootElement
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Here we used &lt;code&gt;Provider&lt;/code&gt; of UsernameContext object that would make the values of this context available to children components. In this case, the children component is &lt;code&gt;&amp;lt;App /&amp;gt;&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;UsernameContext.Provider&lt;/code&gt; expects value prop.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Profile.js
import UsernameContext from "./UsernameContext";
export default function Profile() {
  const { userName } = useContext(UsernameContext);
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;h1&amp;gt;Welcome {userName} &amp;lt;/h1&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Now to consume values of &lt;code&gt;UsernameContext&lt;/code&gt;, we would use &lt;code&gt;useContext&lt;/code&gt; hook. We pass the Context object here that we want to use. In our case we want to use &lt;code&gt;UsernameContext&lt;/code&gt; object.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Note -&lt;/strong&gt; There are cleaner ways of writing context like taking &lt;code&gt;Context.Provider&lt;/code&gt; out of &lt;code&gt;index.js&lt;/code&gt; but I left them as it is for simplicity's purpose.&lt;/p&gt;




&lt;p&gt;Did you see that when we used context we no longer had to touch the &lt;code&gt;&amp;lt;App /&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;Dashboard /&amp;gt;&lt;/code&gt; components? The values came down without having to pass from App/Dashboard. This is the power of context.😎😎😎 &lt;/p&gt;

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

&lt;h5&gt;
  
  
  Woohoo, now we know why does context exist in react? and how to use it?
&lt;/h5&gt;




&lt;p&gt;Some things to point about context - &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Whenever value prop changes in provider, react causes rerender to each consumer components of that respective context.&lt;/li&gt;
&lt;li&gt;If the provider sits at root component then changes in provider causes the whole app to rerender.&lt;/li&gt;
&lt;li&gt;You don't have any means of preventing the rerender cause on every prop change each component subscribed to context is forced to rerender. &lt;/li&gt;
&lt;/ul&gt;




&lt;h4&gt;
  
  
  Now coming to the title of article - Are you using context right?
&lt;/h4&gt;

&lt;p&gt;So this question because people usually say &lt;code&gt;Can we use context+useReducer&lt;/code&gt; instead of &lt;code&gt;redux&lt;/code&gt;? To understand this question let's go back to definition of context by react - &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Context provides a way to pass data through the component tree without having to pass props down manually at every level.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;If you read carefully it clearly says that context helps to &lt;code&gt;pass&lt;/code&gt; the data? Right? It never said that context manages state. But when you use term &lt;code&gt;redux&lt;/code&gt; it means that you're referring to state management.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Speaking of state management, we can say that useState/useReducer does manipulate our state. So they are sort of state management but context never manages your state.  It just passes it down the DOM.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You can think of context as conveyer belt where you put something(some values) on it and then that belt keeps rolling. Whenever those things(values) reach desired station(component where you want to use values), you take them off the belt.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In the whole process, context never stores and manipulates the values.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  So how does redux differ in this scenario?
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Redux also uses context for availing redux store instances to components in DOM tree.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;But in redux, your components can subscribe to part of the whole store and they would only rerender when those values change but this is not the case in context.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;So this feature of redux helps us improve the performance of web apps by controlling the rerender that would happen in-app.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;So it really depends on what is the need of your app. If your app performs frequent updates then using context with useReducer might yield some performance issues in your app. But if you want control over your component updates then redux is the way to go. We got to understand that context wasn't supposed to be used as state management. &lt;/p&gt;




&lt;p&gt;So with this, we've reached the end of the article. I would like to put a statement here that I came across when I was reading this blog by &lt;a href="https://blog.isquaredsoftware.com/2021/01/context-redux-differences/"&gt;Mark Erikson&lt;/a&gt;. Sebastian Markbage(React Core Team) said this about Context -&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;My personal summary is that new context is ready to be used for low frequency unlikely updates (like locale/theme). It's also good to use it in the same way as old context was used. I.e. for static values and then propagate updates through subscriptions. It's not ready to be used as a replacement for all Flux-like state propagation.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Thank you if you read till here!&lt;/strong&gt; 👋&lt;/p&gt;

</description>
      <category>react</category>
      <category>hooks</category>
    </item>
    <item>
      <title>Why thinking asynchronous is important in JS?[Part-2]</title>
      <dc:creator>Aditya Tiwari</dc:creator>
      <pubDate>Fri, 18 Dec 2020 21:18:25 +0000</pubDate>
      <link>https://dev.to/tiwariaditya/why-thinking-asynchronous-is-important-in-js-part-2-1mmb</link>
      <guid>https://dev.to/tiwariaditya/why-thinking-asynchronous-is-important-in-js-part-2-1mmb</guid>
      <description>&lt;p&gt;Hello folks! Now we know the working of JS that we discussed in my last article. Let's understand how synchronous JS code can lead us to problems.&lt;/p&gt;




&lt;ul&gt;
&lt;li&gt;Let's recall one thing -

&lt;code&gt;JS execution waits until current statement executes completely before jumping to next one&lt;/code&gt;

. &lt;/li&gt;
&lt;li&gt;What does the above line mean? That when JS code wants to access resources outside the program, the program control waits until that block of code does its task.&lt;/li&gt;
&lt;li&gt;So what? What's the issue here? it's alright if we wanna access resources like files or accessing local storage(considering JS code in Nodejs).&lt;/li&gt;
&lt;li&gt;Yes, but the issue starts when our code hits outside our machine and goes into the network. There are chances that the URL we wanna visit doesn't exist anymore. Maybe we put wrong URL.&lt;/li&gt;
&lt;li&gt;In the above-mentioned cases, the main execution thread would remain blocked until the request made in network returns. This, in turn, blocks the call stack.&lt;/li&gt;
&lt;li&gt;What would happen now? Since the execution thread and call stack are blocked, any DOM and other events would stop resulting in the freezing of browser.&lt;/li&gt;
&lt;li&gt;Consider an example given below -
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var dummy = $.get("dummy.co.in");
var dummies = $.get("dummies.edu");

console.log(dummy);
console.log(dummies);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;In the snippet given above, there are chances that dummy.co.in or dummies.edu doesn't exist or maybe the server is down right now or maybe the domain has changed.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  What's the issue with the above approach?🤔
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;So it's alright if we didn't get a response right?&lt;/li&gt;
&lt;li&gt;Yes, but the main execution thread was blocked for the whole time when program control was waiting for a response and didn't finish executing later code. &lt;/li&gt;
&lt;li&gt;This is the problem that our program is waiting unnecessarily when we could've finished our remaining code and when the response came back we would've come back and printed the output.&lt;/li&gt;
&lt;li&gt;Therefore synchronous code would lead us to a dead-end where our main execution thread would be blocked and call stack doesn't work. &lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  What can be done to avoid these cases?👀
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;I know you guessed it right. Yes, asynchronous calls.&lt;/li&gt;
&lt;li&gt;Hold on! hold on! I know what are you thinking. If JS is synchronous and single-threaded how would it make asynchronous calls?&lt;/li&gt;
&lt;li&gt;Okay, so do you remember in the last article when we were discussing WebAPIs. I told you that they kinda provide way to write async JS.&lt;/li&gt;
&lt;li&gt;WebAPIs like fetch or setTimeouts() are part of the browser, not v8 but somehow v8 can communicate and access it as well as WebAPIs can access callback queue.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1  console.log("1");
2  setTimeout(function timer(){
3      console.log("2");
4  }, 2000);
5  console.log("3");
6
7  // output
8  1
9  3
10 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Code snippet shows that console.log("3") being last statement executed before console.log("2").&lt;/li&gt;
&lt;li&gt;Because when execution started setTimeout set a timer of 2000 milliseconds inside WebAPIs section of browser. After line 2 program control jumped to line 5 logged 3 and popped the console.log("3") from the stack.&lt;/li&gt;
&lt;li&gt;Once timer of 2 seconds set by setTimeout stopped WebAPI pushed the callback timer() that was passed through setTimeout.&lt;/li&gt;
&lt;li&gt;Now timer() is queued in the callback queue. Event loop checks if stack is empty and pushes the timer() callback onto stack which in turn pushes console.log("2") onto stack.&lt;/li&gt;
&lt;li&gt;There we saw how console.log("2") was put on hold asynchronously but we didn't block the call stack. Call stack went ahead and put console.log("3") before the timer would have finished.&lt;/li&gt;
&lt;/ul&gt;




&lt;ul&gt;
&lt;li&gt;Just like this, promises are also helpful to achieve async behavior.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   fetch("https://jsonplaceholder.typicode.com/todos")
  .then((res) =&amp;gt; res.json())
  .then((json) =&amp;gt; console.log(json))
  .catch((err) =&amp;gt; console.log(err));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;In the above snippets, we're calling a REST api and we don't know when would it return object. So instead of blocking the main thread, we're chaining it with other methods of promise like &lt;code&gt;Promise.then()&lt;/code&gt; and &lt;code&gt;Promise.catch()&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;After an unknown time, when the response comes back. We can do whatever with it inside &lt;code&gt;Promise.then()&lt;/code&gt; which is catching the promise thrown by &lt;code&gt;fetch()&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;So never block your main execution thread and event loop&lt;/code&gt;&lt;/p&gt;




&lt;p&gt;&lt;em&gt;So see you in next one👋&lt;/em&gt;&lt;br&gt;
&lt;strong&gt;Thank you if you read this long!&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
    </item>
    <item>
      <title>Why thinking asynchronous is important in JS?[Part-1]</title>
      <dc:creator>Aditya Tiwari</dc:creator>
      <pubDate>Fri, 18 Dec 2020 19:55:09 +0000</pubDate>
      <link>https://dev.to/tiwariaditya/why-thinking-asynchronous-is-important-in-js-part-1-4boi</link>
      <guid>https://dev.to/tiwariaditya/why-thinking-asynchronous-is-important-in-js-part-1-4boi</guid>
      <description>&lt;p&gt;Hey folks! I have been learning about javascript lately. I came across a lot of terms like v8, DOM, callbacks, stack, etc. So in this article, we are going to look at how javascript works internally and supports asynchronous calls when it is synchronous. Why thinking asynchronous is important?&lt;/p&gt;




&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fl5z6psrfhgumtpdage8z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fl5z6psrfhgumtpdage8z.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above diagram shows actually how javascript is executed in the browser. So now let's divide the above diagram into parts and see what each of them exactly does.&lt;/p&gt;




&lt;h2&gt;
  
  
  1. v8 engine -
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Any programming or scripting language requires the bare machine to run. Consider when we run our c/c++ code or golang code, they've got compilers. Compilers convert them into machine code and then machine code is executed by processors.&lt;/li&gt;
&lt;li&gt;So JS also requires some means through which the browser understands JS code.&lt;/li&gt;
&lt;li&gt;v8 is a JS engine that was developed for chromium-based browsers and written in c++.&lt;/li&gt;
&lt;li&gt;This is responsible for running JS and code execution in browsers. Call Stack and heap is part of v8 that helps JS to execute commands. &lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  2. Heap -
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Heap is the memory area where memory management of JS code happens.&lt;/li&gt;
&lt;li&gt;This is where variables and functions are stored in form of objects by JS.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  3. Call Stack -
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;We have come to the exciting part now. This is where JS code is actually executed in the code execution phase.&lt;/li&gt;
&lt;li&gt;We know, JS is interpreted language. So JS is executed line-by-line. &lt;/li&gt;
&lt;li&gt;Let's see an example.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fa2axw9kjf7b6nembsq0b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fa2axw9kjf7b6nembsq0b.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Do you know how would call stack execute it? Let's see.&lt;/li&gt;
&lt;li&gt;Before we start talking about the stack, let's just understand how execution starts.

&lt;ul&gt;
&lt;li&gt;Whenever JS starts execution it always happens in 2 phases. Memory Creation and Code Execution.&lt;/li&gt;
&lt;li&gt;In the memory creation phase, the whole code is checked. Variable and function declarations are hoisted.&lt;/li&gt;
&lt;li&gt;After memory creation, in the code execution phase initialization of variables and calling of function happens.&lt;/li&gt;
&lt;li&gt;So coming back to the above example, when the interpreter reaches line one, sees a console.log statement, pushes this statement on the stack. &lt;/li&gt;
&lt;li&gt;As we know stack is last-in-first-out, interpreter waits until this statement executes. Once it finishes 2 things are done. First, the previous statement on stack is popped from call stack and then it moves to line no 3 and finds a function declaration.&lt;/li&gt;
&lt;li&gt;Since it's not invoked, interpreter moves to line no 7. Here printName is invoked so control goes back to line no 3 and this function would to pushed in stack now. now this function has a console.log() statement. So that also pushed to stack. &lt;/li&gt;
&lt;li&gt;Once the console is logged, the previous 2 items on call stack i.e. console.log() and function printName() would be popped respectively.&lt;/li&gt;
&lt;li&gt;So this is how code execution occurs in JS. So a quick summary - Statements are pushed onto stack, program control waits until execution of top item on stack finishes since JS being single-threaded in can not move furthers until the current line of execution happens and then moving on to next statement;&lt;/li&gt;
&lt;li&gt;It makes sense right? that's why javascript is synchronous single-threaded language.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;
  
  
  4. WebAPIs -
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The WebAPIs aren't part of v8. They are provided by browsers. That means WebAPIs are available to each browser you are working on.&lt;/li&gt;
&lt;li&gt;DOM actions, fetch() request, XML objects and other functions like setTimeout() aren't part of v8 but they are given to us via WebAPIs that are incorporated with browsers.&lt;/li&gt;
&lt;li&gt;Till now we were saying that JS is synchronous, right? No?&lt;/li&gt;
&lt;li&gt;But WebAPIs are part of browsers that helps JS to make it asynchronous. We would come here why does it make JS asynchronous? in the next part of this article&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  5. Callback Queue -
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Whenever some WebAPIs are called or any DOM event happens, that event triggers a function that acts as a utility function to perform tasks after that event. This function is usually known as a callback function.&lt;/li&gt;
&lt;li&gt;Since they trigger after the event, they aren't pushed to call stack but they are queued in callback queue to wait until being pushed on the call stack.&lt;/li&gt;
&lt;li&gt;Asynchronous calls also return callbacks or promises that are queued right here in the queue.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  6. Event Loop -
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;As we discussed in above section that callbacks aren't pushed on call stack but are queued in the callback queue.&lt;/li&gt;
&lt;li&gt;So how would callbacks waiting in queue know that it is my turn to go on stack and finish the job instead of waiting here. They need a way, aren't they?&lt;/li&gt;
&lt;li&gt;This is where the event loop plays the role.&lt;/li&gt;
&lt;li&gt;The only job of event loop is

&lt;code&gt;Wait until call stack is empty. If call stack is empty push one callback onto stack and start waiting until the call stack is empty again&lt;/code&gt;

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




&lt;p&gt;So we've covered the parts that help JS to execute and saw why JS is synchronous and single-threaded. In next part, we would see why synchronous nature of JS is not good and what are harms of it.&lt;/p&gt;




&lt;p&gt;See you in the next part.😃&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>eventloop</category>
    </item>
    <item>
      <title>Do you know autoboxing in JS?</title>
      <dc:creator>Aditya Tiwari</dc:creator>
      <pubDate>Sat, 17 Oct 2020 16:42:16 +0000</pubDate>
      <link>https://dev.to/tiwariaditya/do-you-know-autoboxing-in-js-4ng0</link>
      <guid>https://dev.to/tiwariaditya/do-you-know-autoboxing-in-js-4ng0</guid>
      <description>&lt;p&gt;Hey folks 👋! &lt;br&gt;
    I am currently learning javascript. I came across something that I never heard before. I learned about autoboxing and wrapper. I knew wrappers are used in java but had no clue they exist in javascript. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Look at the code given below -&lt;/p&gt;
&lt;/blockquote&gt;

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

&lt;p&gt;Strange isn't it? We declared &lt;code&gt;name&lt;/code&gt; and &lt;code&gt;age&lt;/code&gt; variables. We didn't declare objects. So where are these methods and attributes coming from?&lt;/p&gt;




&lt;p&gt;So whenever we try to access functions from the prototype of primitives, under the hood, javascript wraps these variables to its wrapper types.&lt;/p&gt;

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

&lt;p&gt;See above, this is how they are initialized as temporary wrapper objects by the javascript engine. &lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;What are wrapper objects?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Wrapper objects convert primitive data types to object type. This is the same case in java where primitives are converted to objects by wrappers classes because collection framework doesn't store primitives. The primitives in javascript are &lt;code&gt;number&lt;/code&gt;, &lt;code&gt;string&lt;/code&gt;, &lt;code&gt;boolean&lt;/code&gt;, &lt;code&gt;undefined&lt;/code&gt;, &lt;code&gt;null&lt;/code&gt; and &lt;code&gt;symbol&lt;/code&gt;. Their wrapper objects are &lt;code&gt;Number&lt;/code&gt;, &lt;code&gt;String&lt;/code&gt;, &lt;code&gt;Boolean&lt;/code&gt; as follows.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;How are they temporary?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Whenever this conversion from primitive to object happens, the objects are dumped after a single-use.&lt;/p&gt;

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

&lt;p&gt;We are overriding &lt;code&gt;toUpperCase()&lt;/code&gt; and returning "toUppercase", still &lt;code&gt;toUpperCase()&lt;/code&gt; was not overridden. Do you know why? Yes because they are temporary. They are invoked when we are accessing functions in the prototype and dumped immediately. &lt;/p&gt;




&lt;p&gt;I'd be coming with a follow-up article where we'd discuss what's need for autoboxing.&lt;br&gt;
&lt;strong&gt;Thank You for reading!&lt;/strong&gt;  &lt;/p&gt;

</description>
      <category>javascript</category>
    </item>
    <item>
      <title>How to fix npm create-react-app stuck at 'found 0 vulnerabilities'?</title>
      <dc:creator>Aditya Tiwari</dc:creator>
      <pubDate>Fri, 03 Jul 2020 16:27:04 +0000</pubDate>
      <link>https://dev.to/tiwariaditya/how-to-fix-npm-create-react-app-stuck-at-found-0-vulnerabilities-323k</link>
      <guid>https://dev.to/tiwariaditya/how-to-fix-npm-create-react-app-stuck-at-found-0-vulnerabilities-323k</guid>
      <description>&lt;p&gt;I started learning to React recently. I was following The Net Ninjas' over YouTube. So as I was instructed to install Node.js, I downloaded and installed it (64-bit). I ran npx &lt;code&gt;create-react-app app&lt;/code&gt; which is responsible for creating a react app with boilerplate required.&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;strong&gt;What problem did this command cause?&lt;/strong&gt;
&lt;/h1&gt;

&lt;p&gt;Once I ran this command, It would start fetching data from the repository. The folder would be created with the project name having &lt;code&gt;package.json&lt;/code&gt;, &lt;code&gt;package-lock.json&lt;/code&gt;, and &lt;code&gt;node_modules&lt;/code&gt; folder. After this, the command would run and get stuck at &lt;code&gt;found 0 vulnerabilities&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If you are using cmd on windows&lt;/p&gt;
&lt;/blockquote&gt;

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

&lt;blockquote&gt;
&lt;p&gt;If you are using bash&lt;/p&gt;
&lt;/blockquote&gt;

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

&lt;h1&gt;
  
  
  &lt;strong&gt;How to fix this?&lt;/strong&gt;
&lt;/h1&gt;

&lt;p&gt;I was searching a lot and found out people were asking on StackOverflow about this fix but none of them had answers and&lt;br&gt;
even if some had but they were not useful. So I kept searching on GitHub under issues and came across some fixes. There are 2 ways to solve this.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Fix 1 (Easy One)-&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;I don't know why but this problem is observed with 12.16.2-x64.msi node version. If you installed x64 version then you just need to uninstall this version and install x32 bit version. This fix should solve your problem.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Fix 2-&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;If you don't want to reinstall node and continue with the current version then this fix would work. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Open a new cmd window and run &lt;code&gt;resmon&lt;/code&gt; command.
This command opens resource monitor and you would see something like this -&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;Once you could see resource monitor. You need to start looking for cmd.exe processes (because there would be more than one cmd.exe based on how many windows you have got open) which are suspended. You could see if a process is suspended by right-clicking on it.
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Sr11cBkA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/g5v55gjbaqa23bhx5r8r.PNG" alt="Alt Text"&gt;
&lt;/li&gt;
&lt;li&gt;If you find any cmd.exe suspended resume it. Your cmd process would also get resumed. There might be a case where cmd again stops, you just follow the above steps again. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The only problem with this is you might need to follow these steps 1 or 2 times more when using npx create-react-app before the error goes away.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Conclusion -&lt;/em&gt;&lt;br&gt;
This was my first blog. This problem was faced by so many people out there so I tried to write this blog. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Thank You&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>npm</category>
      <category>react</category>
    </item>
  </channel>
</rss>
