<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Md. Pial Ahamed]]></title><description><![CDATA[I love R&D and learn through new challenges. Here I share some glimpses of my knowledge.]]></description><link>https://blog.pial.dev</link><generator>RSS for Node</generator><lastBuildDate>Sun, 31 May 2026 07:29:49 GMT</lastBuildDate><atom:link href="https://blog.pial.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[JavaScript Scope Chain In Simple Word]]></title><description><![CDATA[Many people get confused about how the scope chain works. When I started learning JS it was very overwhelming for me by thinking why there is so much topic than C/C++/Java about simple things. But later I found many of the topics are also exist for o...]]></description><link>https://blog.pial.dev/javascript-scope-chain-in-simple-word</link><guid isPermaLink="true">https://blog.pial.dev/javascript-scope-chain-in-simple-word</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[programming languages]]></category><category><![CDATA[Programming Blogs]]></category><dc:creator><![CDATA[Md. Pial Ahamed]]></dc:creator><pubDate>Thu, 21 Jan 2021 15:17:49 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1611238881909/Ar9IGG01l.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Many people get confused about how the scope chain works. When I started learning JS it was very overwhelming for me by thinking why there is so much topic than C/C++/Java about simple things. But later I found many of the topics are also exist for other languages too. Just I didn't found any courses/tutorials during learning those languages. <strong>Scope Chain</strong> is one of them.</p>
<h1 id="scope">Scope</h1>
<blockquote>
<p>If you are sitting in your room it's your scope you can take/access anything from your room. A scope defines your accessible area.</p>
<h1 id="scope-chain">Scope Chain</h1>
<p>When you will go to another room it will be another scope but you might or might not have permission to access/take something. Scope chain defines where you can go to access when you need something.</p>
<h1 id="local-scope">Local Scope</h1>
<p>Own scope</p>
<h1 id="global-scope">Global Scope</h1>
<p>Where you start to write your code in the JS document</p>
<h1 id="parent-scope">Parent Scope</h1>
<p>Before your birth you were inside of your mother/parent like that when function is inside of another function/scope will call that above scope Parent Scope.</p>
</blockquote>
<p>Every scope is always connected to one or more scope except the global scope.</p>
<h1 id="how-scope-chain-works">How Scope Chain Works?</h1>
<h4 id="part-1">PART-1</h4>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1611061930000/d7DG3aMo1.png" alt="Code For Part-1" /></p>
<blockquote>
<p>All codes are available at the end of the article</p>
</blockquote>
<p>Here you can access from your room things like 💻️ 📱️ 🍕️ because it's in your room. You can't get the vault key because that is in another separate room. Similarly your parent can't take your pizza/mobile/laptop. But you and your parents can access house 🏚️ key. 
Imagine you need your <code>houseKey</code> so you will look in <em>your room</em> first if you don't find then <em>you will come out of your room</em> and see <em>it's in outside of your room or not</em>. You won't go to look to your parents room because you won't be able to enter there because you don't have permission (in the above scenario).</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1611165225679/U9gt3zBWB.png" alt="Code For Part-1" />
Similarly, JS will look first  If there is any <code>houseKey</code> in the <strong>current scope</strong> if doesn't find then it will look in its <em>parent scope</em>. If not found in that scope and it will continue to go to that scope's parent scope and so on until finding it. That's where the chain word comes. Chains are connected to each other. Like that by connecting scope to another scope <em>Scope Chain</em> creates. In this case, <code>yourRoom</code> parent scope is <strong>Global Scope</strong>. When it finds in the global scope <code>houseKey</code> it stops looking and use that.</p>
<h4 id="part-2">PART-2</h4>
<p>You might say wait! You just said I can't get the vault key just because it's not in my room, <code>houseKey</code> is not in my room also!! what happened to that logic🙄️. But the thing is even it's not in your or parents room both rooms are inside of the house. Your room was not inside of your parents room that's why you didn't have access to that. So, what will happen if it was!!. Yes, then you would have definitely access🤑️.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1611238855778/BS4QaUFZF.png" alt="Code For Part-2" />
Here for the <code>vaultKey</code> JS will look first in the <code>yourRoom</code>'s scope then when it will see it is not in there it will go to it's parent scope which is <code>parentRoom</code>'s scope and when it will find it will use that and will stop looking if there is another <code>vaultKey</code> in other scopes.</p>
<p>🎁️ <strong>Bonus</strong>: If you are curious 🙄️ why even scope concept exists then look for this topic <code>The Principle of Least Privilege (PoLP)</code>.</p>
<p>This is my first technical blog article. I hope it was useful for you. Feel free to give me suggestion to improve my writing. </p>
<p><strong>Code</strong>:</p>
<h4 id="part-1">PART-1</h4>
<pre><code><span class="hljs-comment">//Global Scope</span>
<span class="hljs-comment">//Accessible to your parents and you</span>
<span class="hljs-keyword">let</span> houseKey = <span class="hljs-string">"🔑️"</span>

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">parentsRoom</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-comment">//🧔️👩️your parents scope</span>
    <span class="hljs-keyword">let</span> vaultKey = <span class="hljs-string">"🗝️"</span>
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">yourRoom</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-comment">//🧒️/👧️your scope</span>
    <span class="hljs-keyword">let</span> yourThings = [<span class="hljs-string">"💻️"</span>,<span class="hljs-string">"📱️"</span>,<span class="hljs-string">"🍕️"</span>]
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"This is my 🏚️ key"</span>, houseKey)
}
</code></pre><h4 id="part-2">PART-2</h4>
<pre><code><span class="hljs-comment">//Global Scope</span>
<span class="hljs-comment">//Accessible to your parents and you</span>
<span class="hljs-keyword">let</span> houseKey = <span class="hljs-string">"🔑️"</span>

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">parentsRoom</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-comment">//🧔️👩️your parents scope</span>
    <span class="hljs-keyword">let</span> vaultKey = <span class="hljs-string">"🗝️"</span>
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">yourRoom</span>(<span class="hljs-params"></span>) </span>{
        <span class="hljs-comment">//🧒️/👧️your scope</span>
        <span class="hljs-keyword">let</span> yourThings = [<span class="hljs-string">"💻️"</span>,<span class="hljs-string">"📱️"</span>,<span class="hljs-string">"🍕️"</span>]
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"This is my 🏚️ key "</span>, houseKey)
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"🤑️ I have access vault key "</span>, vaultKey)
    }
    parentsRoom.yourRoom = yourRoom <span class="hljs-comment">//so that yourRoom function can be accessed from outside</span>
}
parentsRoom()
parentsRoom.yourRoom()
</code></pre>]]></content:encoded></item></channel></rss>