<?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: Vishal Singh</title>
    <description>The latest articles on DEV Community by Vishal Singh (@vishalcsx).</description>
    <link>https://dev.to/vishalcsx</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%2F3588299%2F510c0b60-5ca0-46c1-811b-51bb8200e767.jpg</url>
      <title>DEV Community: Vishal Singh</title>
      <link>https://dev.to/vishalcsx</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/vishalcsx"/>
    <language>en</language>
    <item>
      <title>Demystifying JavaScript: Execution Contexts, Hoisting, Scopes &amp; Closures</title>
      <dc:creator>Vishal Singh</dc:creator>
      <pubDate>Tue, 20 Jan 2026 10:42:57 +0000</pubDate>
      <link>https://dev.to/vishalcsx/demystifying-javascript-execution-contexts-hoisting-scopes-closures-pj</link>
      <guid>https://dev.to/vishalcsx/demystifying-javascript-execution-contexts-hoisting-scopes-closures-pj</guid>
      <description>&lt;h2&gt;
  
  
  What Are Execution Contexts?
&lt;/h2&gt;

&lt;p&gt;Execution Contexts manage code complexity for the JavaScript engine, similar to how functions manage authoring complexity. The engine creates them in two types: Global Execution Context at startup and Function Execution Contexts on function invocation.&lt;br&gt;
Each context has a Creation phase and an Execution phase. In Creation, the engine sets up the global object (&lt;code&gt;window&lt;/code&gt; in browsers, &lt;code&gt;global&lt;/code&gt; in Node), &lt;code&gt;this&lt;/code&gt;, memory for variables/functions, initializes variables to &lt;code&gt;undefined&lt;/code&gt;, and loads function declarations fully. [&lt;/p&gt;
&lt;h2&gt;
  
  
  Hoisting Explained
&lt;/h2&gt;

&lt;p&gt;Hoisting occurs during Creation when variable declarations get &lt;code&gt;undefined&lt;/code&gt; and functions get fully loaded, before line-by-line execution. This explains why &lt;code&gt;console.log(name)&lt;/code&gt; before &lt;code&gt;var name = 'Tyler';&lt;/code&gt; outputs &lt;code&gt;undefined&lt;/code&gt;—no physical movement happens.&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;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;name: &lt;/span&gt;&lt;span class="dl"&gt;'&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="c1"&gt;// undefined&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="s1"&gt;getUser: &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;getUser&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// [Function: getUser]&lt;/span&gt;
&lt;span class="kd"&gt;var&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="s1"&gt;Tyler&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;getUser&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="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Function declarations hoist completely, unlike variables. &lt;/p&gt;

&lt;h2&gt;
  
  
  Function Execution Contexts
&lt;/h2&gt;

&lt;p&gt;Function Contexts create an &lt;code&gt;arguments&lt;/code&gt; object instead of a global object, plus &lt;code&gt;this&lt;/code&gt;, and hoist locals. Invocations push new contexts onto the Call Stack; completion pops them off, as JavaScript is single-threaded. &lt;/p&gt;

&lt;p&gt;Arguments become local variables, and inner variables stay in their context. For example, passing &lt;code&gt;handle&lt;/code&gt; to &lt;code&gt;getURL(handle)&lt;/code&gt; adds it locally alongside globals. &lt;/p&gt;

&lt;h2&gt;
  
  
  Scopes and Scope Chain
&lt;/h2&gt;

&lt;p&gt;Scope defines variable accessibility as the current Execution Context. Locals can't access outer variables post-pop, causing &lt;code&gt;ReferenceError&lt;/code&gt; for &lt;code&gt;console.log(bar)&lt;/code&gt; after &lt;code&gt;foo()&lt;/code&gt; where &lt;code&gt;bar&lt;/code&gt; is local to &lt;code&gt;foo&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;If absent locally, the engine climbs the Scope Chain to parents up to Global. Multiple &lt;code&gt;name&lt;/code&gt; variables in different contexts resolve locally first: outputs &lt;code&gt;undefined&lt;/code&gt;, &lt;code&gt;Jordyn&lt;/code&gt;, &lt;code&gt;Jake&lt;/code&gt;, &lt;code&gt;Tyler&lt;/code&gt;. &lt;/p&gt;

&lt;h2&gt;
  
  
  Closures in Action
&lt;/h2&gt;

&lt;p&gt;Closures let inner functions access outer scopes post-parent execution via a persistent Closure Scope. In &lt;code&gt;makeAdder(x)&lt;/code&gt;, the returned &lt;code&gt;inner&lt;/code&gt; retains &lt;code&gt;x&lt;/code&gt; despite &lt;code&gt;makeAdder&lt;/code&gt;'s context popping.&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;makeAdder&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="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;inner&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;y&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;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;y&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;add5&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;makeAdder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&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="nf"&gt;add5&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;  &lt;span class="c1"&gt;// 7&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  for More infomation visit to
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://fireship.dev/ultimate-guide-to-execution-contexts-hoisting-scopes-and-closures-in-javascript" rel="noopener noreferrer"&gt;https://fireship.dev/ultimate-guide-to-execution-contexts-hoisting-scopes-and-closures-in-javascript&lt;/a&gt;&lt;/p&gt;

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