<?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: Pujarini Jena</title>
    <description>The latest articles on DEV Community by Pujarini Jena (@pujarini).</description>
    <link>https://dev.to/pujarini</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%2F628391%2Ff7ed90ba-bc1d-4c73-8dae-36eef2e3516b.jpeg</url>
      <title>DEV Community: Pujarini Jena</title>
      <link>https://dev.to/pujarini</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/pujarini"/>
    <language>en</language>
    <item>
      <title>let's talk about scope</title>
      <dc:creator>Pujarini Jena</dc:creator>
      <pubDate>Mon, 10 May 2021 18:53:32 +0000</pubDate>
      <link>https://dev.to/pujarini/let-s-talk-about-scope-3719</link>
      <guid>https://dev.to/pujarini/let-s-talk-about-scope-3719</guid>
      <description>&lt;p&gt;What is the first thing that pops in your mind when you ask someone what is scope?&lt;br&gt;
Something related to accessibility maybe.&lt;br&gt;
There are various concepts around this scope which  are interesting and make you pull your hair at the same time. &lt;br&gt;
Most of the javascript developers will tell you 'let' and 'const' are block scoped.&lt;/p&gt;

&lt;p&gt;What is scope exactly?&lt;br&gt;
Scope defines the accessibility of any variable or function.&lt;/p&gt;

&lt;p&gt;There are 2 types of scope:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Global scope &lt;/li&gt;
&lt;li&gt;Local scope&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;What is global scope?&lt;br&gt;
Variables or functions which can be accessed by anyone outside the block or inside the block.&lt;/p&gt;

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

&lt;p&gt;What is local scope?&lt;br&gt;
Local scope is of many types. Some are&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Block scope&lt;/li&gt;
&lt;li&gt;Function scope&lt;/li&gt;
&lt;li&gt;Lexical Scope&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Block scope&lt;/p&gt;

&lt;p&gt;what is a block?&lt;br&gt;
Block is a group of statements where Javascript expects a single statement.&lt;br&gt;
you can have if block, switch block, for-loop block etc These are all blocks.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6NPObrie--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nxuidfqg5zsc5ktysxdz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6NPObrie--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nxuidfqg5zsc5ktysxdz.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What is block scope then?&lt;br&gt;
Variables or functions which are accessible within this block.&lt;br&gt;
Simple isn't it?&lt;/p&gt;

&lt;p&gt;Here Javascript has gotcha&lt;/p&gt;

&lt;p&gt;We can declare variables as var, let, and const.&lt;br&gt;
Earlier I mentioned let and const are block scoped&lt;/p&gt;

&lt;p&gt;let see how these 3 work inside a block&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--56W5zizG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ya7odgev7f336eyd8ryk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--56W5zizG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ya7odgev7f336eyd8ryk.png" alt="image"&gt;&lt;/a&gt;&lt;br&gt;
What will be the output? Let's see that&lt;/p&gt;

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

&lt;p&gt;What if I want to access the 3 variables outside the block?&lt;br&gt;
Can i?&lt;br&gt;
Let's see that in the working&lt;/p&gt;

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

&lt;p&gt;Can you see that it says 'Reference Error - b is not defined' but it shows the value of a which is of var type where as let is not accessible.&lt;br&gt;
Now can you relate why let and const are block-scoped.&lt;/p&gt;

&lt;p&gt;Let's see how JS engine works for let and const &lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--pxQxEW_v--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tbyq0kpkxtnz5xkiugqo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pxQxEW_v--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tbyq0kpkxtnz5xkiugqo.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Initially, the variables let and const are placed in block scope and the var is placed in the global scope and initialized with 'undefined'&lt;/p&gt;

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

&lt;p&gt;As we execute the code line by line then variables get initialized with the values&lt;/p&gt;

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

&lt;p&gt;Here can you see that block scope vanishes? &lt;br&gt;
As we are out of the block the block scope also gets deleted due to which let and const are not accessible outside the block and hence called 'block scope'.&lt;/p&gt;

&lt;p&gt;Function scope&lt;/p&gt;

&lt;p&gt;Variables having scope within a function&lt;/p&gt;

&lt;p&gt;Code snippet for above statement&lt;/p&gt;

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

&lt;p&gt;we cannot access any variable outside the function scope.&lt;/p&gt;

&lt;p&gt;Lexical scope&lt;/p&gt;

&lt;p&gt;Child scope variables can access parent scope variables.&lt;br&gt;
It is said that the Child function is lexically bound by the parent function.&lt;/p&gt;

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

&lt;p&gt;The image illustrates the lexical scope&lt;/p&gt;

&lt;p&gt;Let's see in code.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--X4aGYwLG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tc9gu7p4otk9k4bm41us.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--X4aGYwLG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tc9gu7p4otk9k4bm41us.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;the innerMost function has access to inner function variables as well as outer function variables too.&lt;/p&gt;

&lt;p&gt;Please note that var is an issue creator it overrides the parent assignment for the variable in block scope and not in function scope hence let and const were introduced&lt;/p&gt;

&lt;p&gt;The below image is an illustration in block scope&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--T-24Jpqm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ctqjtkw55u10mzd3ognb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--T-24Jpqm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ctqjtkw55u10mzd3ognb.png" alt="image"&gt;&lt;/a&gt;&lt;br&gt;
This image is for function scope&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--d39amMz_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cqedj9alxly46e50atlr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--d39amMz_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cqedj9alxly46e50atlr.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hoping the article was quite knowledgeable for the ones reading this. Please add your comments if I have missed anything or mentioned something wrong.&lt;/p&gt;

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