<?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: Sanu Ranjan</title>
    <description>The latest articles on DEV Community by Sanu Ranjan (@sanuranjan).</description>
    <link>https://dev.to/sanuranjan</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%2F4004454%2Fffb35b17-265a-4a59-81f7-799f9eb14f08.png</url>
      <title>DEV Community: Sanu Ranjan</title>
      <link>https://dev.to/sanuranjan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sanuranjan"/>
    <language>en</language>
    <item>
      <title>JavaScript Closures: How They Actually Work</title>
      <dc:creator>Sanu Ranjan</dc:creator>
      <pubDate>Fri, 26 Jun 2026 21:13:35 +0000</pubDate>
      <link>https://dev.to/sanuranjan/javascript-closures-how-they-actually-work-376g</link>
      <guid>https://dev.to/sanuranjan/javascript-closures-how-they-actually-work-376g</guid>
      <description>&lt;p&gt;&lt;em&gt;A quick note before we start: this post is for beginners trying to understand how closures actually work. I have done my best as per my limited knowledge to explain what happens under the hood, so if I have gotten anything wrong, please correct me in the comments.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;When I was first learning closures, I could recite the definition but still could not explain why they actually work. Information that goes past the definition and into the "why" and "How" is sometimes harder to find than expected. One of my dear friends finally explained it to me in a way that made it click, so I thought I should pass it on for any beginner going through the same thing.&lt;/p&gt;

&lt;p&gt;The definition we have probably heard: an inner function remembers variables from its outer function, even after the outer function has finished running. That is correct, but it is only the surface. The real question is, what is so special here? Why does that memory survive at all? The answer lives in how the JavaScript engine handles memory behind the scenes.&lt;/p&gt;

&lt;p&gt;Here is the example we will use the whole way through:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;innerCounter&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&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;innerCounter&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;myCounter1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nf"&gt;myCounter1&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// 1&lt;/span&gt;
&lt;span class="nf"&gt;myCounter1&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// 2&lt;/span&gt;

&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;myCounter2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nf"&gt;myCounter2&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// 3&lt;/span&gt;
&lt;span class="nf"&gt;myCounter1&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// 3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I'm using &lt;code&gt;var&lt;/code&gt; here just to make the breakdown easier to visualize.&lt;/p&gt;

&lt;h2&gt;
  
  
  How the Engine Runs the Code
&lt;/h2&gt;

&lt;p&gt;Contrary to the popular belief that JavaScript is just a simple, line-by-line interpreted language, the engine handles the code in distinct steps. There are two phases:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Compilation Phase:&lt;/strong&gt; the engine scans the code and registers all variable declarations and function definitions before anything runs. For each function, it creates a function object in memory, and the function's name points to that object.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Execution Phase:&lt;/strong&gt; the code finally runs, line by line.&lt;/p&gt;

&lt;p&gt;Separately from these, there's garbage collection: the cleanup process that runs in the background. Normally, once a function finishes executing, its local execution scope is freed from memory so it doesn't waste space. With a closure, that cleanup gets blocked. The executed function's local execution scope persists, letting the inner function reach back up the scope chain and grab what it needs.&lt;/p&gt;

&lt;p&gt;Quick recap of the example above: &lt;code&gt;counter&lt;/code&gt; takes a parameter &lt;code&gt;n&lt;/code&gt; (default 0), stores it in &lt;code&gt;count&lt;/code&gt;, and returns an inner function &lt;code&gt;innerCounter&lt;/code&gt; that increments and logs &lt;code&gt;count&lt;/code&gt;. We create &lt;code&gt;myCounter1&lt;/code&gt; from &lt;code&gt;counter()&lt;/code&gt; and &lt;code&gt;myCounter2&lt;/code&gt; from &lt;code&gt;counter(a)&lt;/code&gt;, and the outputs come out as 1, 2, 3, 3. Now let's see how.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: Mapping the Global Scope
&lt;/h2&gt;

&lt;p&gt;Before a single line runs, JavaScript sets up the global scope in memory during compilation, registering the names &lt;code&gt;a&lt;/code&gt;, &lt;code&gt;myCounter1&lt;/code&gt;, and &lt;code&gt;myCounter2&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;When it hits the &lt;code&gt;counter&lt;/code&gt; definition, it creates a function object for it in memory, and the name &lt;code&gt;counter&lt;/code&gt; points to that object. Here's the secret sauce: every function gets a hidden internal property (often written &lt;code&gt;[[Scope]]&lt;/code&gt;) that points back to the environment where it was born. For &lt;code&gt;counter&lt;/code&gt;, that points to the global scope.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: Creating myCounter1
&lt;/h2&gt;

&lt;p&gt;Now execution starts. It sets &lt;code&gt;a = 2&lt;/code&gt;, then calls &lt;code&gt;counter()&lt;/code&gt; with no argument.&lt;/p&gt;

&lt;p&gt;Invoking the function spins up a brand new local execution context, which points back to wherever its function definition's &lt;code&gt;[[Scope]]&lt;/code&gt; is pointing to. Inside this temporary space, a mini-compilation happens:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;n&lt;/code&gt; defaults to 0.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;count&lt;/code&gt; is registered and initialized to 0.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;innerCounter&lt;/code&gt; function is defined and stored and &lt;code&gt;innerCounter&lt;/code&gt; holds reference to its function definition, and its hidden &lt;code&gt;[[Scope]]&lt;/code&gt; points right back to this local scope.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Finally, &lt;code&gt;counter()&lt;/code&gt; returns &lt;code&gt;innerCounter&lt;/code&gt;, which returns the reference of its function definition and we store it in &lt;code&gt;myCounter1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Normally the local execution scope of &lt;code&gt;counter()&lt;/code&gt; would now be wiped by the garbage collector. But JavaScript has a golden rule: if an environment can still be reached from the global scope, it can't be garbage collected. Since &lt;code&gt;myCounter1&lt;/code&gt; points to the inner function's definition, and the inner function points back to this local scope &lt;code&gt;counter()&lt;/code&gt;, a bridge remains. The memory survives.&lt;/p&gt;

&lt;h2&gt;
  
  
  Climbing the Scope Chain
&lt;/h2&gt;

&lt;p&gt;Now we call &lt;code&gt;myCounter1()&lt;/code&gt;. A fresh local execution scope is created for the function execution which points back to wherever its &lt;code&gt;[[Scope]]&lt;/code&gt; is pointing to, here it points to &lt;code&gt;counter()&lt;/code&gt;'s local execution scope which is still in memory.&lt;/p&gt;

&lt;p&gt;Then in execution phase for this function it encounters &lt;code&gt;count++&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;First it checks its own local context for &lt;code&gt;count&lt;/code&gt;. Nothing there. So it follows up its scope chain into the preserved parent memory the &lt;code&gt;counter()&lt;/code&gt; execution context, finds &lt;code&gt;count&lt;/code&gt; at 0, increments it to 1 in that parent scope, and logs 1, and then &lt;code&gt;myCounter1()&lt;/code&gt; local execution context is garbage collected.&lt;/p&gt;

&lt;p&gt;The second call &lt;code&gt;myCounter1()&lt;/code&gt; repeats exactly: it looks locally, finds nothing, climbs the chain, finds &lt;code&gt;count&lt;/code&gt; now at 1, increments it to 2, and logs 2.&lt;/p&gt;

&lt;p&gt;We get updated value here all because &lt;code&gt;counter()&lt;/code&gt;'s local execution scope is still not garbage collected.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: Total Isolation with myCounter2
&lt;/h2&gt;

&lt;p&gt;What happens when we create &lt;code&gt;myCounter2&lt;/code&gt;?&lt;/p&gt;

&lt;p&gt;Because we invoke &lt;code&gt;counter&lt;/code&gt; fresh, JavaScript generates a completely separate, second local execution context. Here &lt;code&gt;n&lt;/code&gt; receives the value of &lt;code&gt;a&lt;/code&gt; (which is 2), so &lt;code&gt;count&lt;/code&gt; starts at 2.&lt;/p&gt;

&lt;p&gt;Calling &lt;code&gt;myCounter2()&lt;/code&gt; climbs its own scope chain, finds its own &lt;code&gt;count&lt;/code&gt; at 2, increments it, and outputs 3.&lt;/p&gt;

&lt;p&gt;To prove the two environments share nothing, look at the final &lt;code&gt;myCounter1()&lt;/code&gt;. It ignores whatever &lt;code&gt;myCounter2&lt;/code&gt; did, goes right back to its original environment, finds its old value of 2, increments it to 3, and logs 3.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;At the end of the day, that's all a closure is. The outer function's local execution scope escapes garbage collection because the inner function's definition, whose reference is present in the global scope, keeps a live, reachable path back to the outer function's local execution scope via &lt;code&gt;[[Scope]]&lt;/code&gt;. That persistent connection is what keeps the data alive across separate instances.&lt;/p&gt;

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