<?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: Gaurav Singh</title>
    <description>The latest articles on DEV Community by Gaurav Singh (@gauravsingh7x).</description>
    <link>https://dev.to/gauravsingh7x</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%2F3943940%2Fd7aa87a2-7717-4d82-a724-896d146d3cd2.png</url>
      <title>DEV Community: Gaurav Singh</title>
      <link>https://dev.to/gauravsingh7x</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/gauravsingh7x"/>
    <language>en</language>
    <item>
      <title># JavaScript Hoisting — What Actually Happens Behind the Scenes ?</title>
      <dc:creator>Gaurav Singh</dc:creator>
      <pubDate>Wed, 08 Jul 2026 13:52:38 +0000</pubDate>
      <link>https://dev.to/gauravsingh7x/-javascript-hoisting-what-actually-happens-behind-the-scenes--21od</link>
      <guid>https://dev.to/gauravsingh7x/-javascript-hoisting-what-actually-happens-behind-the-scenes--21od</guid>
      <description>&lt;h1&gt;
  
  
  Demystifying Hoisting: What Really Happens Inside the JavaScript Engine
&lt;/h1&gt;

&lt;p&gt;Most JavaScript developers have heard the common statement: &lt;em&gt;"JavaScript moves variables and functions to the top of your file."&lt;/em&gt; While this explanation is widespread, it is technically incorrect. Hoisting is &lt;strong&gt;not&lt;/strong&gt; code movement. Nothing is physically shifted around in your source file. Instead, hoisting is the direct result of how the JavaScript engine prepares memory before executing a single line of your code.&lt;/p&gt;

&lt;p&gt;Once you understand what happens internally during this preparation step, hoisting becomes one of the easiest JavaScript concepts to grasp.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why Hoisting Exists: The Two-Pass Model
&lt;/h2&gt;

&lt;p&gt;JavaScript does not just execute your code immediately, line by line. Before execution starts, the JavaScript engine performs a crucial preparation step where it scans your code, identifies variables and functions, and allocates memory for them.&lt;/p&gt;

&lt;p&gt;This process happens in two distinct phases:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Memory Creation Phase:&lt;/strong&gt; The engine scans the code and allocates memory slots for all declarations. &lt;strong&gt;This is when hoisting happens.&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Execution Phase:&lt;/strong&gt; The engine runs your code line by line, assigning values and executing functions.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  A Simple Real-Life Analogy
&lt;/h3&gt;

&lt;p&gt;Imagine students entering an examination hall. Before they begin writing their papers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Answer sheets are distributed.&lt;/li&gt;
&lt;li&gt;Roll numbers are assigned to desks.&lt;/li&gt;
&lt;li&gt;Seating arrangements are completed.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Only after all preparations are finished does the actual exam begin. JavaScript behaves exactly the same way. Before executing code, variables and functions are discovered, and memory space is allocated.&lt;/p&gt;




&lt;h2&gt;
  
  
  Defining Hoisting Accurately
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The Beginner-Friendly Definition:&lt;/strong&gt; Hoisting is JavaScript's behavior of making declarations available before execution starts.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The Technically Accurate Definition:&lt;/strong&gt; Hoisting is the memory allocation process performed during the creation phase of an Execution Context.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Deep Dive: 4 Core Examples of Hoisting
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Example 1: Variable Hoisting with &lt;code&gt;var&lt;/code&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&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;a&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;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;undefined

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

&lt;/div&gt;



&lt;p&gt;Many beginners expect a &lt;code&gt;ReferenceError&lt;/code&gt; here. Why does JavaScript print &lt;code&gt;undefined&lt;/code&gt; instead?&lt;/p&gt;

&lt;h4&gt;
  
  
  What Happens Internally?
&lt;/h4&gt;

&lt;p&gt;During the &lt;strong&gt;Memory Creation Phase&lt;/strong&gt;, the engine encounters &lt;code&gt;var a = 10;&lt;/code&gt;. It splits this into declaration and assignment, allocates memory for &lt;code&gt;a&lt;/code&gt;, and automatically initializes it with a default value of &lt;code&gt;undefined&lt;/code&gt;.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Variable&lt;/th&gt;
&lt;th&gt;Initial Value&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;a&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;undefined&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;When the &lt;strong&gt;Execution Phase&lt;/strong&gt; starts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Step 1:&lt;/strong&gt; &lt;code&gt;console.log(a);&lt;/code&gt; runs. At this exact moment, &lt;code&gt;a&lt;/code&gt; holds the value &lt;code&gt;undefined&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Step 2:&lt;/strong&gt; &lt;code&gt;a = 10;&lt;/code&gt; runs, updating the value in memory from &lt;code&gt;undefined&lt;/code&gt; to &lt;code&gt;10&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is why &lt;code&gt;var&lt;/code&gt; variables can be accessed before their formal declaration without throwing an error.&lt;/p&gt;




&lt;h3&gt;
  
  
  Example 2: Function Hoisting
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;sayHello&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;sayHello&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="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Hello

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

&lt;/div&gt;



&lt;p&gt;Function declarations are treated differently from variables. During the creation phase, JavaScript stores the &lt;strong&gt;entire function definition&lt;/strong&gt; in memory right away.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Name&lt;/th&gt;
&lt;th&gt;Stored Value&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;sayHello&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;function sayHello() { ... }&lt;/code&gt; (Complete Function)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Because the complete function is fully hoisted and available before execution begins, you can safely invoke it at the very top of your script.&lt;/p&gt;




&lt;h3&gt;
  
  
  Example 3: Function Expressions
&lt;/h3&gt;

&lt;p&gt;What happens when you combine a function with a variable declaration?&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="nf"&gt;sayHi&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;sayHi&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;function &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="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hi&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;TypeError: sayHi is not a function

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

&lt;/div&gt;



&lt;p&gt;This surprises many developers. Why does it fail?&lt;/p&gt;

&lt;p&gt;During the &lt;strong&gt;Memory Creation Phase&lt;/strong&gt;, the engine treats &lt;code&gt;var sayHi&lt;/code&gt; like any other variable. It allocates memory and initializes it to &lt;code&gt;undefined&lt;/code&gt;. The function assignment itself is ignored until the execution phase.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Variable&lt;/th&gt;
&lt;th&gt;Value&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;sayHi&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;undefined&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;During execution, the engine reaches &lt;code&gt;sayHi();&lt;/code&gt; and translates it to &lt;code&gt;undefined();&lt;/code&gt;. Because &lt;code&gt;undefined&lt;/code&gt; is not a callable function data type, JavaScript throws a &lt;code&gt;TypeError&lt;/code&gt;. The variable container is hoisted, but the function assignment is not.&lt;/p&gt;




&lt;h3&gt;
  
  
  Example 4: Hoisting with &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&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;a&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;a&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ReferenceError: Cannot access 'a' before initialization

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

&lt;/div&gt;



&lt;p&gt;At first glance, it appears that &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; are not hoisted. &lt;strong&gt;But that is a misconception—they are hoisted.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When the engine scans code during the memory phase, it allocates space for &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; variables. However, unlike &lt;code&gt;var&lt;/code&gt;, they are &lt;strong&gt;not&lt;/strong&gt; initialized with &lt;code&gt;undefined&lt;/code&gt;. Instead, they are placed in an uninitialized state known as the &lt;strong&gt;Temporal Dead Zone (TDZ)&lt;/strong&gt;.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Variable&lt;/th&gt;
&lt;th&gt;State&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;a&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Uninitialized (Temporal Dead Zone)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Attempting to read or write to a variable while it sits in the TDZ results in a &lt;code&gt;ReferenceError&lt;/code&gt;. The variable only becomes safely usable &lt;em&gt;after&lt;/em&gt; the execution flow reaches the line where it is formally initialized.&lt;/p&gt;




&lt;h2&gt;
  
  
  Quick Reference Summary
&lt;/h2&gt;

&lt;p&gt;The following tables break down how different declarations are treated during the internal memory creation phase.&lt;/p&gt;

&lt;h3&gt;
  
  
  Declaration Behaviors
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;&lt;code&gt;var&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;let&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;const&lt;/code&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Hoisted&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Initial Value&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;undefined&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Uninitialized&lt;/td&gt;
&lt;td&gt;Uninitialized&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Access Before Dec.&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;undefined&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;ReferenceError&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;ReferenceError&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Scope&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Function&lt;/td&gt;
&lt;td&gt;Block&lt;/td&gt;
&lt;td&gt;Block&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Memory Phase Blueprint
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Type&lt;/th&gt;
&lt;th&gt;Memory Phase State&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;&lt;code&gt;var&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;undefined&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;&lt;code&gt;function&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Stores Entire Function&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;&lt;code&gt;let&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;uninitialized (TDZ)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;&lt;code&gt;const&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;uninitialized (TDZ)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  How JavaScript Executes Code: Behind the Scenes
&lt;/h2&gt;

&lt;p&gt;To truly tie this all together, we have to look at the execution environment itself. When a web browser encounters a script tag, it passes the source code down to its respective engine (such as Google Chrome's &lt;strong&gt;V8&lt;/strong&gt;, Mozilla Firefox's &lt;strong&gt;SpiderMonkey&lt;/strong&gt;, or Safari's &lt;strong&gt;JavaScriptCore&lt;/strong&gt;).&lt;/p&gt;

&lt;p&gt;The engine processes your file using a mechanism called the &lt;strong&gt;Execution Context&lt;/strong&gt;—an environment wrapper that manages the execution variables, functions, scopes, and the &lt;code&gt;this&lt;/code&gt; evaluation.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Call Stack
&lt;/h3&gt;

&lt;p&gt;JavaScript handles execution contexts using a &lt;strong&gt;Call Stack&lt;/strong&gt;. Think of it as a stack of dishes where the last item added is the first one to be cleared:&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;one&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;two&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="nf"&gt;two&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="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello&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="nf"&gt;one&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

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

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Global Execution Context&lt;/strong&gt; is created and pushed to the bottom of the stack when the script loads.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;one()&lt;/code&gt; is invoked $\rightarrow$ Its execution context is pushed onto the stack.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;two()&lt;/code&gt; is invoked inside &lt;code&gt;one()&lt;/code&gt; $\rightarrow$ Its execution context is pushed to the top.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;two()&lt;/code&gt; finishes $\rightarrow$ Popped off the stack.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;one()&lt;/code&gt; finishes $\rightarrow$ Popped off the stack.&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Complete Internal Flow Example
&lt;/h2&gt;

&lt;p&gt;Let's look at a complete program to see how memory creation and execution phases pair together:&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;var&lt;/span&gt; &lt;span class="nx"&gt;x&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;function&lt;/span&gt; &lt;span class="nf"&gt;square&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&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;n&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="p"&gt;}&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;square&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&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;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Phase 1: Memory Creation
&lt;/h3&gt;

&lt;p&gt;Before running line one, the global memory environment maps out your declarations:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;x&lt;/code&gt; is registered and set to &lt;code&gt;undefined&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;square&lt;/code&gt; is registered, storing its entire block logic.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;result&lt;/code&gt; is registered and set to &lt;code&gt;undefined&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Phase 2: Code Execution
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Line 1:&lt;/strong&gt; &lt;code&gt;x&lt;/code&gt; is assigned the value &lt;code&gt;5&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Line 2:&lt;/strong&gt; The engine skips over the function declaration since it's already stored.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Line 3:&lt;/strong&gt; &lt;code&gt;square(x)&lt;/code&gt; is evaluated. A new local function execution context is spawned on the Call Stack, passing &lt;code&gt;5&lt;/code&gt; into the parameter &lt;code&gt;n&lt;/code&gt;. It computes $5 \times 5$, returns &lt;code&gt;25&lt;/code&gt;, and destroys its local environment context. &lt;code&gt;result&lt;/code&gt; is updated to &lt;code&gt;25&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Line 4:&lt;/strong&gt; Outputs &lt;code&gt;25&lt;/code&gt; directly to the console.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Wrapping Up: The Ultimate Mental Model
&lt;/h2&gt;

&lt;p&gt;Whenever your JavaScript programs run, remember this four-step mental checklist:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Create&lt;/strong&gt; an Execution Context.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Scan&lt;/strong&gt; for variable and function declarations.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Allocate&lt;/strong&gt; memory slots (initializing variables based on their keywords).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Execute&lt;/strong&gt; the remaining source code line by line.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Hoisting isn't a magical code-lifting feature. It is simply the JavaScript engine taking a moment to set up its memory grid before letting your program loose!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>hoisting</category>
      <category>tdz</category>
      <category>temporaldeadzome</category>
    </item>
    <item>
      <title># Understanding JavaScript Closures Through Call Stack, Heap Memory &amp; `[[Scopes]]`</title>
      <dc:creator>Gaurav Singh</dc:creator>
      <pubDate>Fri, 26 Jun 2026 06:27:39 +0000</pubDate>
      <link>https://dev.to/gauravsingh7x/-understanding-javascript-closures-through-call-stack-heap-memory-scopes-5gha</link>
      <guid>https://dev.to/gauravsingh7x/-understanding-javascript-closures-through-call-stack-heap-memory-scopes-5gha</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;Closures aren't magic—they're simply JavaScript's way of keeping data alive when a function still needs it.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Every JavaScript developer has heard statements like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;"A closure is a function that remembers variables from its outer scope."&lt;/li&gt;
&lt;li&gt;"The inner function closes over variables."&lt;/li&gt;
&lt;li&gt;"Closures preserve the lexical environment."&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But...&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;How does a function actually remember variables?&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Where are those variables stored after the outer function finishes?&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Why doesn't JavaScript delete them?&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's go beyond the textbook definition and see what actually happens inside the JavaScript engine.&lt;/p&gt;




&lt;h1&gt;
  
  
  The Two Main Players Inside the JavaScript Engine
&lt;/h1&gt;

&lt;p&gt;Whenever a function executes, two important memory areas are involved:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Call Stack&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Heap Memory&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Understanding closures is really about understanding &lt;strong&gt;where JavaScript stores variables&lt;/strong&gt; and &lt;strong&gt;why some of them survive after a function finishes executing.&lt;/strong&gt;&lt;/p&gt;




&lt;h1&gt;
  
  
  1. What Happens in a Normal Function?
&lt;/h1&gt;

&lt;p&gt;Consider a simple function:&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;greet&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;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;JavaScript&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="nf"&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="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When &lt;code&gt;greet()&lt;/code&gt; executes:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;An &lt;strong&gt;Execution Context&lt;/strong&gt; is created.&lt;/li&gt;
&lt;li&gt;Local variables (&lt;code&gt;name&lt;/code&gt;) belong to that execution.&lt;/li&gt;
&lt;li&gt;The execution context is pushed onto the &lt;strong&gt;Call Stack&lt;/strong&gt;.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Call Stack

┌─────────────────────────┐
│ Execution Context       │
│ name = "JavaScript"     │
└─────────────────────────┘
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After the function finishes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The execution context is popped off the stack.&lt;/li&gt;
&lt;li&gt;No code references &lt;code&gt;name&lt;/code&gt; anymore.&lt;/li&gt;
&lt;li&gt;It becomes eligible for &lt;strong&gt;Garbage Collection&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Everything is cleaned up.&lt;/p&gt;




&lt;h1&gt;
  
  
  2. What Changes When a Closure Is Created?
&lt;/h1&gt;

&lt;p&gt;Now look at this example:&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;outerFunction&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;count&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="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;innerFunction&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;innerFunction&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;counter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;outerFunction&lt;/span&gt;&lt;span class="p"&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;counter&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nf"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1
2
3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But wait...&lt;/p&gt;

&lt;p&gt;&lt;code&gt;outerFunction()&lt;/code&gt; already finished executing.&lt;/p&gt;

&lt;p&gt;Its execution context has been removed from the Call Stack.&lt;/p&gt;

&lt;p&gt;So why is &lt;code&gt;count&lt;/code&gt; still available?&lt;/p&gt;

&lt;p&gt;That's where &lt;strong&gt;closures&lt;/strong&gt; come in.&lt;/p&gt;




&lt;h1&gt;
  
  
  Behind the Scenes
&lt;/h1&gt;

&lt;p&gt;When JavaScript notices that &lt;code&gt;innerFunction&lt;/code&gt; uses &lt;code&gt;count&lt;/code&gt;, it understands:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"This variable will still be needed after &lt;code&gt;outerFunction()&lt;/code&gt; returns."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Instead of letting &lt;code&gt;count&lt;/code&gt; disappear with the execution context, the engine keeps it alive.&lt;/p&gt;

&lt;p&gt;Three important things happen behind the scenes.&lt;/p&gt;




&lt;h1&gt;
  
  
  Step A — Captured Variables Are Kept in Heap Memory
&lt;/h1&gt;

&lt;p&gt;The variables that are captured by an inner function are stored inside an internal object called the &lt;strong&gt;Lexical Environment&lt;/strong&gt; (often called the Closure Context).&lt;/p&gt;

&lt;p&gt;Conceptually, you can think of it like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="err"&gt;Heap&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;Memory&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="err"&gt;Lexical&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;Environment&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="err"&gt;count:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Unlike the Call Stack, heap memory isn't destroyed when the function returns.&lt;/p&gt;

&lt;p&gt;This allows the captured variables to stay alive.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Engines don't literally "move" variables from the stack to the heap. Instead, captured variables are stored in a heap-allocated lexical environment so they can outlive the function call.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h1&gt;
  
  
  Step B — The Secret Link (&lt;code&gt;[[Scopes]]&lt;/code&gt;)
&lt;/h1&gt;

&lt;p&gt;Every function internally carries a hidden reference called:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[[Scopes]]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This hidden reference points to the lexical environment containing the variables it needs.&lt;/p&gt;

&lt;p&gt;Conceptually:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;counter
   │
   ▼
[[Scopes]]
   │
   ▼
Lexical Environment
{
   count: 0
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Even after &lt;code&gt;outerFunction()&lt;/code&gt; has finished, the returned function still knows exactly where &lt;code&gt;count&lt;/code&gt; lives.&lt;/p&gt;




&lt;h1&gt;
  
  
  Step C — The Scope Chain
&lt;/h1&gt;

&lt;p&gt;Later, when you execute:&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="nf"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;JavaScript performs variable lookup in this order:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Check the function's local scope.&lt;/li&gt;
&lt;li&gt;If not found, follow &lt;code&gt;[[Scopes]]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Search the lexical environment.&lt;/li&gt;
&lt;li&gt;Continue upward until the variable is found.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This lookup process is called the &lt;strong&gt;Scope Chain&lt;/strong&gt;.&lt;/p&gt;




&lt;h1&gt;
  
  
  Visualizing the Entire Process
&lt;/h1&gt;

&lt;h3&gt;
  
  
  Before &lt;code&gt;outerFunction()&lt;/code&gt; Returns
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Call Stack

┌────────────────────────────┐
│ outerFunction()            │
│ count = 0                  │
│ innerFunction()            │
└────────────────────────────┘
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  After Returning
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Call Stack

┌──────────────────────┐
│ Global Execution     │
└──────────────────────┘


Heap Memory

┌────────────────────────┐
│ Lexical Environment    │
│ count = 0              │
└────────────────────────┘

        ▲
        │
     [[Scopes]]
        │
        ▼

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

&lt;/div&gt;



&lt;p&gt;The execution context is gone.&lt;/p&gt;

&lt;p&gt;The variable survives because the returned function still references it.&lt;/p&gt;




&lt;h1&gt;
  
  
  A Simple Analogy
&lt;/h1&gt;

&lt;p&gt;Imagine:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;outerFunction()&lt;/code&gt; is a hotel room.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;count&lt;/code&gt; is an important document.&lt;/li&gt;
&lt;li&gt;Before checking out, you place the document in a secure locker (Heap Memory).&lt;/li&gt;
&lt;li&gt;You hand the locker key (&lt;code&gt;[[Scopes]]&lt;/code&gt;) to your child (&lt;code&gt;innerFunction&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Even after the hotel room is empty, your child still has the key and can access the document whenever needed.&lt;/p&gt;

&lt;p&gt;That's exactly how closures work.&lt;/p&gt;




&lt;h1&gt;
  
  
  Why Are Closures Useful?
&lt;/h1&gt;

&lt;h2&gt;
  
  
  1. Data Privacy
&lt;/h2&gt;

&lt;p&gt;Closures let you create private variables.&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;createCounter&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;count&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="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;increment&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="p"&gt;},&lt;/span&gt;

        &lt;span class="nf"&gt;getCount&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;count&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Nobody outside can directly modify &lt;code&gt;count&lt;/code&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  2. Maintaining State
&lt;/h2&gt;

&lt;p&gt;Closures allow functions to remember information between calls.&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;counter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;outerFunction&lt;/span&gt;&lt;span class="p"&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;counter&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nf"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The value of &lt;code&gt;count&lt;/code&gt; persists without using global variables.&lt;/p&gt;




&lt;h1&gt;
  
  
  Key Takeaways
&lt;/h1&gt;

&lt;p&gt;✔ A closure is created when an inner function uses variables from its outer scope.&lt;/p&gt;

&lt;p&gt;✔ JavaScript keeps those captured variables alive inside a heap-allocated lexical environment.&lt;/p&gt;

&lt;p&gt;✔ The returned function stores a hidden reference called &lt;code&gt;[[Scopes]]&lt;/code&gt; to that environment.&lt;/p&gt;

&lt;p&gt;✔ As long as the function exists, the captured variables cannot be garbage collected.&lt;/p&gt;

&lt;p&gt;✔ Variable lookup through these linked environments is called the &lt;strong&gt;Scope Chain&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Final Thought
&lt;/h2&gt;

&lt;p&gt;Closures aren't magic.&lt;/p&gt;

&lt;p&gt;They're simply the JavaScript engine preserving the variables that are still needed.&lt;/p&gt;

&lt;p&gt;Once you understand the relationship between the &lt;strong&gt;Call Stack&lt;/strong&gt;, &lt;strong&gt;Heap Memory&lt;/strong&gt;, &lt;strong&gt;Lexical Environment&lt;/strong&gt;, and the hidden &lt;strong&gt;&lt;code&gt;[[Scopes]]&lt;/code&gt;&lt;/strong&gt; reference, closures become one of the most elegant and powerful features of JavaScript.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>closure</category>
      <category>callstack</category>
      <category>heapmemory</category>
    </item>
    <item>
      <title>JavaScript Lexical Scope, Lexical Environment, Scope Chain &amp; Closure — Explained Like the JS Engine</title>
      <dc:creator>Gaurav Singh</dc:creator>
      <pubDate>Thu, 25 Jun 2026 17:23:17 +0000</pubDate>
      <link>https://dev.to/gauravsingh7x/javascript-lexical-scope-lexical-environment-scope-chain-closure-explained-like-the-js-engine-2hle</link>
      <guid>https://dev.to/gauravsingh7x/javascript-lexical-scope-lexical-environment-scope-chain-closure-explained-like-the-js-engine-2hle</guid>
      <description>&lt;p&gt;Most developers memorize the definitions of &lt;strong&gt;Lexical Scope&lt;/strong&gt;, &lt;strong&gt;Lexical Environment&lt;/strong&gt;, &lt;strong&gt;Scope Chain&lt;/strong&gt;, and &lt;strong&gt;Closure&lt;/strong&gt; without understanding what actually happens inside the JavaScript engine.&lt;/p&gt;

&lt;p&gt;In this article, we'll build these concepts step by step and understand them from the engine's perspective.&lt;/p&gt;




&lt;p&gt;What Does "Lexical" Mean?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Lexical&lt;/strong&gt; means:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;"Based on where things are written in the source code."&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;JavaScript determines variable access by looking at the &lt;strong&gt;physical location&lt;/strong&gt; of functions in your code—not where they are called.&lt;/p&gt;




&lt;h1&gt;
  
  
  &lt;a href="https://medium.com/@gauravsingh7x/javascript-lexical-scope-lexical-environment-scope-chain-closure-explained-like-the-js-engine-dc090baa273a" rel="noopener noreferrer"&gt;1. Lexical Scope&lt;/a&gt;
&lt;/h1&gt;

&lt;p&gt;A function can access:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Its own variables&lt;/li&gt;
&lt;li&gt;Variables from its outer scope&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The scope is determined by &lt;strong&gt;where a function is defined&lt;/strong&gt;, &lt;strong&gt;not where it is executed&lt;/strong&gt;.&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;let&lt;/span&gt; &lt;span class="nx"&gt;dynamicVar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;I'm Global&lt;/span&gt;&lt;span class="dl"&gt;"&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;checkScope&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="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;dynamicVar&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="nf"&gt;anotherFunction&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;dynamicVar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;I'm Local&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nf"&gt;checkScope&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Function is called here&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;anotherFunction&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Output
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;I'm Global
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Why?
&lt;/h2&gt;

&lt;p&gt;Before execution begins, JavaScript analyzes the source code and decides which variables every function can access.&lt;/p&gt;

&lt;p&gt;This rule is called &lt;strong&gt;Lexical (Static) Scope&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Although &lt;code&gt;checkScope()&lt;/code&gt; is called inside &lt;code&gt;anotherFunction()&lt;/code&gt;, it was &lt;strong&gt;defined in the global scope&lt;/strong&gt;, so it only has access to the global &lt;code&gt;dynamicVar&lt;/code&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  What If JavaScript Used Dynamic Scope?
&lt;/h2&gt;

&lt;p&gt;If JavaScript used &lt;strong&gt;Dynamic Scope&lt;/strong&gt; (which depends on where a function is called), the output would be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;I'm Local
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But JavaScript &lt;strong&gt;doesn't&lt;/strong&gt; work this way.&lt;/p&gt;




&lt;h2&gt;
  
  
  Lexical Scope Search Path
&lt;/h2&gt;

&lt;p&gt;Before execution starts, JavaScript builds a blueprint of variable accessibility by analyzing the code structure.&lt;/p&gt;

&lt;p&gt;Think of it as answering this question:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;"Which variables can each function access?"&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That blueprint is called &lt;strong&gt;Lexical Scope&lt;/strong&gt;.&lt;/p&gt;




&lt;h1&gt;
  
  
  &lt;a href="https://medium.com/@gauravsingh7x/javascript-lexical-scope-lexical-environment-scope-chain-closure-explained-like-the-js-engine-dc090baa273a" rel="noopener noreferrer"&gt;2. Lexical Environment&lt;/a&gt;
&lt;/h1&gt;

&lt;p&gt;A &lt;strong&gt;Lexical Environment&lt;/strong&gt; is an internal object created whenever JavaScript starts executing a scope.&lt;/p&gt;

&lt;p&gt;It stores:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Local variables&lt;/li&gt;
&lt;li&gt;Function declarations&lt;/li&gt;
&lt;li&gt;A reference to its parent lexical environment&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It consists of:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Environment Record&lt;/strong&gt; → Stores variables and functions&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Outer Reference&lt;/strong&gt; → Points to the parent lexical environment&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Together, these make variable lookup possible.&lt;/p&gt;




&lt;h1&gt;
  
  
  Scope Chain
&lt;/h1&gt;

&lt;p&gt;The &lt;strong&gt;Scope Chain&lt;/strong&gt; is formed by connecting lexical environments through their &lt;strong&gt;Outer References&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;When JavaScript can't find a variable in the current scope, it walks up this chain until it either finds the variable or reaches the global scope.&lt;/p&gt;




&lt;h2&gt;
  
  
  Easy Way to Remember
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Lexical Scope&lt;/strong&gt; is the rule that defines what a function can access.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Lexical Environment&lt;/strong&gt; is the place where those variables are actually stored.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Interview Definition
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Lexical Scope defines what a function can access, while the Lexical Environment stores those variables and maintains the relationship between parent and child scopes.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Example
&lt;/h2&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;globalVar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;I am Global&lt;/span&gt;&lt;span class="dl"&gt;"&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;outerFunction&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;outerVar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;I am Outer&lt;/span&gt;&lt;span class="dl"&gt;"&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;innerFunction&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;innerVar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;I am Local&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="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;innerVar&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;outerVar&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;globalVar&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;innerFunction&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;outerFunction&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Output
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;I am Local
I am Outer
I am Global
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  What Happens Behind the Scenes?
&lt;/h1&gt;

&lt;p&gt;When &lt;code&gt;innerFunction()&lt;/code&gt; starts executing:&lt;/p&gt;

&lt;p&gt;JavaScript creates a brand new &lt;strong&gt;Lexical Environment&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;It contains:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Lexical Environment (innerFunction)

Environment Record
------------------
innerVar

Outer Reference
      ↓
Lexical Environment (outerFunction)

Environment Record
------------------
outerVar

Outer Reference
      ↓
Global Lexical Environment

Environment Record
------------------
globalVar
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now suppose JavaScript needs &lt;code&gt;outerVar&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The lookup process becomes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Current Scope
      ↓
Parent Scope
      ↓
Global Scope
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This lookup path is called the &lt;strong&gt;Scope Chain&lt;/strong&gt;.&lt;/p&gt;




&lt;h1&gt;
  
  
  &lt;a href="https://medium.com/@gauravsingh7x/javascript-lexical-scope-lexical-environment-scope-chain-closure-explained-like-the-js-engine-dc090baa273a" rel="noopener noreferrer"&gt;3. Closure&lt;/a&gt;
&lt;/h1&gt;

&lt;p&gt;A &lt;strong&gt;Closure&lt;/strong&gt; is formed when a function &lt;strong&gt;remembers and retains access to its outer lexical environment even after the outer function has finished execution.&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;a href="https://www.linkedin.com/in/gauravsingh7x/" rel="noopener noreferrer"&gt;In My Style&lt;/a&gt;
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;When we return an inner function, pass it as a callback, or store it in a variable, and it carries its lexical environment along with it, that is called a Closure.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Example
&lt;/h2&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;outerFunction&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;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&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;innerFunction&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;a&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;a&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;innerFunction&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;fn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;outerFunction&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="nf"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nf"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nf"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Output
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;11
12
13
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  Why Doesn't &lt;code&gt;a&lt;/code&gt; Disappear?
&lt;/h1&gt;

&lt;p&gt;Normally, when &lt;code&gt;outerFunction()&lt;/code&gt; finishes execution, its execution context is removed from the call stack.&lt;/p&gt;

&lt;p&gt;But JavaScript notices that &lt;strong&gt;&lt;code&gt;innerFunction&lt;/code&gt; still needs &lt;code&gt;a&lt;/code&gt;.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Instead of destroying that memory, JavaScript keeps the lexical environment alive.&lt;/p&gt;

&lt;p&gt;The returned function holds a hidden reference to it.&lt;/p&gt;

&lt;p&gt;This preserved relationship is called a &lt;strong&gt;Closure&lt;/strong&gt;.&lt;/p&gt;




&lt;h1&gt;
  
  
  One Snap &amp;gt; 100 Closure Definitions 📸
&lt;/h1&gt;

&lt;p&gt;Someone asks:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;"Where is the closure?"&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Open Chrome DevTools:&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="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;dir&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Expand:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="err"&gt;Scopes&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="err"&gt;Closure&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;(outerFunction)&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="err"&gt;a:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;13&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There it is.&lt;/p&gt;

&lt;p&gt;That hidden &lt;strong&gt;Closure&lt;/strong&gt; object is the preserved lexical environment that keeps your variables alive even after the outer function has returned.&lt;/p&gt;




&lt;p&gt;&lt;a href="https://medium.com/@gauravsingh7x/javascript-lexical-scope-lexical-environment-scope-chain-closure-explained-like-the-js-engine-dc090baa273a" rel="noopener noreferrer"&gt;Final One-Line Difference&lt;/a&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Concept&lt;/th&gt;
&lt;th&gt;Meaning&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Lexical Scope&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Defines what a function can access.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Lexical Environment&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Stores variables and references to parent scopes.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Scope Chain&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;The path JavaScript follows to find variables.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Closure&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;A function together with the preserved lexical environment it remembers after the outer function has finished execution.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h1&gt;
  
  
  &lt;a href="https://medium.com/@gauravsingh7x/javascript-lexical-scope-lexical-environment-scope-chain-closure-explained-like-the-js-engine-dc090baa273a" rel="noopener noreferrer"&gt;Key Takeaways&lt;/a&gt;
&lt;/h1&gt;

&lt;p&gt;⟶ Lexical means &lt;strong&gt;where code is written&lt;/strong&gt;.&lt;br&gt;
⟶ Scope is determined during parsing, not during function calls.&lt;br&gt;
⟶ Every executing scope gets its own Lexical Environment.&lt;br&gt;
⟶ Lexical Environments are connected through Outer References.&lt;br&gt;
⟶ Those references create the Scope Chain.&lt;br&gt;
⟶ A Closure keeps a lexical environment alive as long as a function still references it.&lt;/p&gt;

&lt;p&gt;If this article helped you understand what's happening &lt;strong&gt;inside the JavaScript engine&lt;/strong&gt;, consider leaving a ❤️ and sharing it with fellow developers.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://medium.com/@gauravsingh7x/javascript-lexical-scope-lexical-environment-scope-chain-closure-explained-like-the-js-engine-dc090baa273a" rel="noopener noreferrer"&gt;Happy Coding!&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>lexical</category>
      <category>closure</category>
      <category>lexicalscope</category>
    </item>
  </channel>
</rss>
