<?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: Mahin Ahmed</title>
    <description>The latest articles on DEV Community by Mahin Ahmed (@mahin19524).</description>
    <link>https://dev.to/mahin19524</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4004773%2Fbb1ca128-f3e9-4792-bfa9-50d00b006a61.png</url>
      <title>DEV Community: Mahin Ahmed</title>
      <link>https://dev.to/mahin19524</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mahin19524"/>
    <language>en</language>
    <item>
      <title>I thought I understood JavaScript scopes... until this happened ( Learning journal #1-Variables and Data Types)</title>
      <dc:creator>Mahin Ahmed</dc:creator>
      <pubDate>Sat, 27 Jun 2026 09:14:37 +0000</pubDate>
      <link>https://dev.to/mahin19524/i-thought-i-understood-javascript-scopes-until-this-happened-learning-journal-1-variables-and-20m3</link>
      <guid>https://dev.to/mahin19524/i-thought-i-understood-javascript-scopes-until-this-happened-learning-journal-1-variables-and-20m3</guid>
      <description>&lt;p&gt;Actually, I was learning the fundamentals of programming in JavaScript. I was studying &lt;strong&gt;Variables and Data Types&lt;/strong&gt; where i learnt about &lt;strong&gt;scopes&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;I understood &lt;strong&gt;global&lt;/strong&gt; and &lt;strong&gt;block&lt;/strong&gt; scope variables. We usually declare a block scope variable with &lt;code&gt;{...}&lt;/code&gt; and the variable is only accessible from inside the brackets. If we try working with it outside the brackets then it will throw some errors. &lt;/p&gt;

&lt;p&gt;I was thinking okey thats good cause it seems simple right? Until I stumbled upon an interesting problem.&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;serviceName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Auth_Service&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="nx"&gt;totalRequests&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;100&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;totalRequests&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&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;localTimeout&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="nx"&gt;totalRequests&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;totalRequests&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="c1"&gt;// [Checkpoint A] What are the values of serviceName and totalRequests here?&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// [Checkpoint B] What happens if we try to console.log(localTimeout) here?&lt;/span&gt;
&lt;span class="c1"&gt;// [Checkpoint C] What is the value of totalRequests here?&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I was confused cause we declared the same variable &lt;code&gt;totalRequests&lt;/code&gt; twice. Once in the global scope and once in the block scope. Shouldn't it throw an error of variable double-declaration?&lt;/p&gt;

&lt;p&gt;Then I thougt okey if it doesn't throw an error then it might work, but how? I know we can still access the global variable inside block scope variable. So it might just rewrite the &lt;code&gt;totalRequests&lt;/code&gt; value from 100 to 5, then we are adding 1 with it, so it should be 6. And it did.&lt;/p&gt;

&lt;p&gt;Output:&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;// Checkpoint A&lt;/span&gt;
&lt;span class="nx"&gt;serviceName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Auth_Service&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="nx"&gt;totalRequests&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;

&lt;span class="c1"&gt;// Checkpoint B&lt;/span&gt;
&lt;span class="nx"&gt;ReferenceError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;localTimeout&lt;/span&gt; &lt;span class="nx"&gt;is&lt;/span&gt; &lt;span class="nx"&gt;not&lt;/span&gt; &lt;span class="nx"&gt;defined&lt;/span&gt;

&lt;span class="c1"&gt;// Checkpoint C&lt;/span&gt;
&lt;span class="nx"&gt;totalRequests&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But after trying to reason through it myself, I searched for an explanation and found this:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;because your second let totalRequests = 5; was inside a { ... } block, the engine treats that block as a completely isolated room (a new Lexical Environment).&lt;/p&gt;

&lt;p&gt;When the engine is inside that block and looks for totalRequests, it checks its immediate room first. It finds the local one, uses it, and ignores the outer one. This is a real engineering concept called Variable Shadowing—the inner variable literally casts a shadow over the outer one, hiding it from view. Once the block ends and the room is destroyed, the shadow lifts, and the global totalRequests (which was never touched or modified) is visible again.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So I interpreted the explaination as ,"Block scope prioritizes its own local variable first, so even if there is one already declared globally, it will not count it."&lt;/p&gt;

&lt;p&gt;I am a beginner who is learning JavaScript for the first time and I am currently focusing on strengthening my fundamentals rather than blindly choosing a trending framework. So it was a great funny challenge for me to solve. Though i successfully solved it but my assumptions were incorrect. Maybe I still haven't fully grasped the exact reason yet, if someone is kind enough to help this nerd, I would be too happy to learn it better. &lt;/p&gt;

&lt;p&gt;Thanks for reading this. If I've misunderstood something, I'd genuinely appreciate corrections. I'm documenting my learning journey, and I'd love to improve my understanding. I heard dev community is helpful and generous, I hope someone would help me clear out the confusion I have 😊.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>learning</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
