<?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: Ananya Mohanta</title>
    <description>The latest articles on DEV Community by Ananya Mohanta (@ananya_mohanta_b44a988e76).</description>
    <link>https://dev.to/ananya_mohanta_b44a988e76</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%2F3065812%2Fbbb032c3-2990-42f3-a586-4b803b90803f.png</url>
      <title>DEV Community: Ananya Mohanta</title>
      <link>https://dev.to/ananya_mohanta_b44a988e76</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ananya_mohanta_b44a988e76"/>
    <language>en</language>
    <item>
      <title>Undefined vs Not Defined</title>
      <dc:creator>Ananya Mohanta</dc:creator>
      <pubDate>Tue, 24 Feb 2026 15:30:00 +0000</pubDate>
      <link>https://dev.to/ananya_mohanta_b44a988e76/undefined-and-not-defined-1fp5</link>
      <guid>https://dev.to/ananya_mohanta_b44a988e76/undefined-and-not-defined-1fp5</guid>
      <description>&lt;p&gt;This is one of the most underrated JavaScript concepts. &lt;br&gt;
When I was learning JavaScript, I used to treat errors as noise.&lt;br&gt;
Red text → fix somehow → move on. &lt;/p&gt;

&lt;p&gt;But one error kept showing up again and again:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5jftgkky7k3p7io6805r.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5jftgkky7k3p7io6805r.png" alt=" " width="442" height="53"&gt;&lt;/a&gt;&lt;br&gt;
They sound very similar, but they are &lt;strong&gt;not&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Understanding this one distinction completely changed how I think about JavaScript execution and memory.&lt;/p&gt;

&lt;h2&gt;
  
  
  What undefined actually means
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;undefined&lt;/strong&gt; is a &lt;strong&gt;special keyword&lt;/strong&gt; in JavaScript. &lt;br&gt;
When a variable is created in a JavaScript program, before the code execution a memory is allocated for that variable with &lt;strong&gt;undefined&lt;/strong&gt;.&lt;br&gt;
It literally means:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“JavaScript knows this variable exists, but no value has been assigned yet.”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Example : &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbdlf3wvztsr6w98vis8y.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbdlf3wvztsr6w98vis8y.png" alt=" " width="800" height="220"&gt;&lt;/a&gt;&lt;br&gt;
Output : &lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1ccdohdcqt8ryftqu9ap.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1ccdohdcqt8ryftqu9ap.png" alt=" " width="132" height="29"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Why?&lt;/p&gt;

&lt;p&gt;Because during the &lt;strong&gt;creation phase of execution context&lt;/strong&gt;, JavaScript does this:&lt;/p&gt;

&lt;blockquote&gt;
&lt;ul&gt;
&lt;li&gt;Sees var &lt;strong&gt;x&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Allocates memory for &lt;strong&gt;x&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Assigns a default value → &lt;strong&gt;undefined&lt;/strong&gt;
So when the code runs, &lt;strong&gt;x&lt;/strong&gt; already exists in memory. That’s why &lt;strong&gt;undefined&lt;/strong&gt; is logged in console.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;undefined is not empty&lt;/strong&gt;. It has its own memory space. It's a placeholder which is kept for the time being until the variable is assigned some other value.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why not defined throws an error
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Not defined&lt;/strong&gt; is something which is &lt;strong&gt;not allocated in memory space&lt;/strong&gt;.&lt;br&gt;
For Example : &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1oa7t9n7bw5q5m9fzann.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1oa7t9n7bw5q5m9fzann.png" alt=" " width="800" height="203"&gt;&lt;/a&gt;&lt;br&gt;
Here before the code execution, there is no memory allocated to &lt;strong&gt;y&lt;/strong&gt;. So it throws an error as shown below : &lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2tghxdhm7926fa31v0c5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2tghxdhm7926fa31v0c5.png" alt=" " width="374" height="20"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This error doesn’t mean “value missing”. It means something much stronger:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“JavaScript has no idea what y is.”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;During the creation phase, JavaScript is searching for declarations(&lt;strong&gt;var&lt;/strong&gt;, &lt;strong&gt;functions&lt;/strong&gt;) and allocates memory only for what it finds. Since &lt;strong&gt;y&lt;/strong&gt; was never declared, JS never created space for it.&lt;br&gt;
So when in code execution, JavaScript tries to access it  →  It throws error upfront.&lt;/p&gt;

&lt;h2&gt;
  
  
  The key difference
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;ul&gt;
&lt;li&gt;undefined → memory exists, value doesn’t&lt;/li&gt;
&lt;li&gt;not defined → memory itself doesn’t exist&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is not a runtime guess. It’s decided &lt;strong&gt;before execution even starts&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  How JavaScript decides this internally
&lt;/h2&gt;

&lt;p&gt;Let’s slow this down. Consider this code:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgq9jq5rwkb8zplftck4a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgq9jq5rwkb8zplftck4a.png" alt=" " width="800" height="289"&gt;&lt;/a&gt;&lt;br&gt;
What happens internally?&lt;/p&gt;

&lt;p&gt;In the creation phase, JavaScript stores the value of &lt;strong&gt;x&lt;/strong&gt; as &lt;strong&gt;undefined ** in the memory. **y&lt;/strong&gt; is ignored because it’s not &lt;strong&gt;declared anywhere&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In the execution phase, since &lt;strong&gt;x&lt;/strong&gt; exists in memory so instead of throwing error it prints &lt;strong&gt;undefined&lt;/strong&gt;. &lt;strong&gt;y&lt;/strong&gt; does not exist in memory so JavaScript throws error.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why JavaScript behaves this way
&lt;/h2&gt;

&lt;p&gt;JavaScript is not a line-by-line interpreter.&lt;br&gt;
It works in &lt;strong&gt;two phases&lt;/strong&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Memory allocation&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Execution&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;Errors like &lt;strong&gt;not defined&lt;/strong&gt; are not about timing. They are about &lt;strong&gt;whether memory was ever allocated&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This exact concept becomes stricter with &lt;strong&gt;let&lt;/strong&gt; and &lt;strong&gt;const&lt;/strong&gt;. They do get memory, but JavaScript intentionally blocks access until initialization.&lt;/p&gt;

&lt;h2&gt;
  
  
  Warning
&lt;/h2&gt;

&lt;p&gt;Never do this :&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4w8r688q113r3ueeloyj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4w8r688q113r3ueeloyj.png" alt=" " width="800" height="203"&gt;&lt;/a&gt;&lt;br&gt;
Why? &lt;br&gt;
&lt;strong&gt;undefined&lt;/strong&gt; is a placeholder which is kept inside the variable and it states that the variable is not assigned anything throughout the code. It meant for a specific purpose.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;undefined and not defined are not small differences. They reveal how JavaScript thinks before it runs code.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Two similar words. Two completely different memory states.&lt;/p&gt;

&lt;p&gt;That’s JavaScript.&lt;/p&gt;

&lt;p&gt;If you liked this post, I’d love to hear your thoughts. Share your feedback in the comments and connect with me on &lt;a href="https://www.linkedin.com/in/amohanta" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>learning</category>
      <category>beginners</category>
      <category>writing</category>
    </item>
    <item>
      <title>Hoisting in let and const : Understanding the Temporal Dead Zone</title>
      <dc:creator>Ananya Mohanta</dc:creator>
      <pubDate>Tue, 17 Feb 2026 15:35:00 +0000</pubDate>
      <link>https://dev.to/ananya_mohanta_b44a988e76/hoisting-in-let-and-const-understanding-the-temporal-dead-zone-2a90</link>
      <guid>https://dev.to/ananya_mohanta_b44a988e76/hoisting-in-let-and-const-understanding-the-temporal-dead-zone-2a90</guid>
      <description>&lt;p&gt;When I moved from var to let &amp;amp; const, I honestly thought I had finally understood JavaScript. Until this happened :&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmjmqonuga44uhjqcra1v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmjmqonuga44uhjqcra1v.png" alt=" " width="800" height="220"&gt;&lt;/a&gt;&lt;br&gt;
Boom. Reference error. &lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5bx4n1y2zmvloeoazwc4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5bx4n1y2zmvloeoazwc4.png" alt=" " width="505" height="27"&gt;&lt;/a&gt;&lt;br&gt;
My first reaction was confusion. How did this just happen?&lt;/p&gt;

&lt;p&gt;At that moment, Temporal Dead Zone felt like magic - something I was supposed to accept, not understand.&lt;br&gt;
But once again, the answer wasn't in memorizing rules. It was hidden in the same place where hoisting, scope, and closure made sense earlier.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Execution Context.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  let and const are Hoisted
&lt;/h2&gt;

&lt;p&gt;One of the biggest misconceptions is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“&lt;strong&gt;let and const are not hoisted&lt;/strong&gt;.”&lt;br&gt;
That’s not &lt;strong&gt;true&lt;/strong&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If they were not hoisted, JavaScript would behave like this:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F343ehd8igps9nw0rsdst.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F343ehd8igps9nw0rsdst.png" alt=" " width="800" height="220"&gt;&lt;/a&gt;&lt;br&gt;
The error we would have gotten here:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdz7573flf0nq19zqcs28.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdz7573flf0nq19zqcs28.png" alt=" " width="314" height="20"&gt;&lt;/a&gt;&lt;br&gt;
But instead, we get here:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5bx4n1y2zmvloeoazwc4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5bx4n1y2zmvloeoazwc4.png" alt=" " width="505" height="27"&gt;&lt;/a&gt;&lt;br&gt;
The error message itself is a hint.&lt;br&gt;
JavaScript knows that &lt;strong&gt;"x"&lt;/strong&gt; exists. It &lt;strong&gt;just&lt;/strong&gt; refuses to let us touch it.&lt;br&gt;
So, the real question become:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Why does JavaScript block access if the variable already exists?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  What actually happens before code runs?
&lt;/h2&gt;

&lt;p&gt;Before even a single line of code executes, JavaScript prepares everything behind the scenes.&lt;br&gt;
It decides:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What variables exist.&lt;/li&gt;
&lt;li&gt;Where they belong.&lt;/li&gt;
&lt;li&gt;how memory should be set up.
Now here is the difference in var and let.
&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fux7jjoi0c96jwjjwuzgn.png" alt=" " width="800" height="289"&gt;
Before the code execution, y is assigned a value "undefined" in memory. So when it runs it does not crash. That's why we can access y even before declaring it.
&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6p1p62wqzitclinr9bdi.png" alt=" " width="103" height="24"&gt;
But, for x, if I do the same as above, then it throws a reference error. Because behind the scene:&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;x -&amp;gt; uninitialised&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is not undefined.&lt;br&gt;
In the case of var, it is in the &lt;strong&gt;global scope&lt;/strong&gt; but in case of let and const it is in &lt;strong&gt;script scope&lt;/strong&gt;.&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqrp6j1sc7hyy4dp4ngz9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqrp6j1sc7hyy4dp4ngz9.png" alt=" " width="217" height="82"&gt;&lt;/a&gt;&lt;br&gt;
Even before a single line code execution, let and const are allocated &lt;strong&gt;memory&lt;/strong&gt; but they are stored in a different memory space than global. They are not on the global object.&lt;/p&gt;

&lt;p&gt;This memory space can't be accessed before putting some value in them. This is called &lt;strong&gt;Hoisting for let &amp;amp; const&lt;/strong&gt;. And that's where &lt;strong&gt;Temporal Dead Zone&lt;/strong&gt; begins.&lt;/p&gt;

&lt;h2&gt;
  
  
  Temporal Dead Zone(TDZ) :
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Temporal dead zone is the time since when this let variable is hoisted and until it is initialized with some value. The time between that is called Temporal Dead Zone.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So, &lt;strong&gt;the phase from hoisting until it is assigned some value is known as Temporal Dead Zone&lt;/strong&gt;. So, whenever I try to access x variable in the temporal dead zone, it gives reference error.&lt;br&gt;
Example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq88b8rnwzg5o4dqui3iq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq88b8rnwzg5o4dqui3iq.png" alt=" " width="800" height="220"&gt;&lt;/a&gt;&lt;br&gt;
From the start of the scope until &lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fobzn8xb1bpptluem72gd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fobzn8xb1bpptluem72gd.png" alt=" " width="800" height="203"&gt;&lt;/a&gt;&lt;br&gt;
count lives in Temporal dead zone. It &lt;strong&gt;exists&lt;/strong&gt;, but accessing it is &lt;strong&gt;not allowed&lt;/strong&gt;.&lt;br&gt;
The moment this line executes:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdnxmegrpvp92moh4afr8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdnxmegrpvp92moh4afr8.png" alt=" " width="800" height="203"&gt;&lt;/a&gt;&lt;br&gt;
The Temporal Dead zone ends. From there on, everything works finally.&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7gsxtd50zgpfyi6k77re.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7gsxtd50zgpfyi6k77re.png" alt=" " width="800" height="220"&gt;&lt;/a&gt; This works perfectly.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why accessing before initializing falis
&lt;/h2&gt;

&lt;p&gt;This is the part that clicked me.&lt;br&gt;
JavaScript does not fail because it's confused. It fails on purpose. If JavaScript allows this:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq88b8rnwzg5o4dqui3iq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq88b8rnwzg5o4dqui3iq.png" alt=" " width="800" height="220"&gt;&lt;/a&gt;&lt;br&gt;
Then let could quickly behave like var. That would bring back:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;accidental bugs&lt;/li&gt;
&lt;li&gt;unpredictable behavior&lt;/li&gt;
&lt;li&gt;hard-to-trace issues in large codebases
So instead of returning undefined, JavaScript throws an error early and loudly.
It’s saying:&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;“You’re using something before you’ve clearly defined it.”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;And that’s a good thing.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why TDZ exists at all
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;TDZ exists to enforce discipline in code order&lt;/strong&gt;.&lt;br&gt;
Consider this:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fimyk2lgl7skprtbqkjk8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fimyk2lgl7skprtbqkjk8.png" alt=" " width="800" height="220"&gt;&lt;/a&gt;&lt;br&gt;
Logically, this makes no sense.&lt;br&gt;
TDZ ensures:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;code reads top-to-bottom the way humans expect&lt;/li&gt;
&lt;li&gt;dependencies are declared before usage&lt;/li&gt;
&lt;li&gt;mistakes don’t silently pass&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;TDZ is not a limitation. It’s a &lt;strong&gt;guardrail&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  const and TDZ
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;const&lt;/strong&gt; behaves the same as let in terms of TDZ, but with &lt;strong&gt;one extra rule&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This fails:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F156ppb76oqoodcfhgsf0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F156ppb76oqoodcfhgsf0.png" alt=" " width="800" height="220"&gt;&lt;/a&gt;&lt;br&gt;
Why?&lt;br&gt;
Because const demands &lt;strong&gt;initialization at declaration&lt;/strong&gt;.&lt;br&gt;
TDZ ensures:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;constants are never half-defined&lt;/li&gt;
&lt;li&gt;immutability is respected from the first moment&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This one line removed all confusion:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Hoisting decides existence.&lt;br&gt;
Initialization decides accessibility.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;var → exists + initialized → usable&lt;/li&gt;
&lt;li&gt;let / const → exists + uninitialized → TDZ&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Once I saw it this way, Temporal Dead Zone stopped being magical. It became inevitable.&lt;br&gt;
If &lt;strong&gt;Execution Context&lt;/strong&gt; was the &lt;strong&gt;foundation&lt;/strong&gt;, &lt;strong&gt;Temporal Dead Zone&lt;/strong&gt; is the proof that JavaScript actually cares about &lt;strong&gt;correctness&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;If you liked this post, I’d love to hear your thoughts. Share your feedback in the comments and connect with me on&lt;a href="https://www.linkedin.com/in/amohanta" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>learning</category>
    </item>
    <item>
      <title>Hoisting in JavaScript</title>
      <dc:creator>Ananya Mohanta</dc:creator>
      <pubDate>Tue, 10 Feb 2026 15:30:00 +0000</pubDate>
      <link>https://dev.to/ananya_mohanta_b44a988e76/hoisting-in-javascript-4nkh</link>
      <guid>https://dev.to/ananya_mohanta_b44a988e76/hoisting-in-javascript-4nkh</guid>
      <description>&lt;p&gt;&lt;strong&gt;Why JavaScript Doesn’t Execute Code Top-to-Bottom ?&lt;/strong&gt;&lt;br&gt;
When I started learning JavaScript, hoisting felt like a &lt;strong&gt;bug pretending to be a feature.&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpajf1hfjgzsc4w5lmkm1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpajf1hfjgzsc4w5lmkm1.png" alt=" " width="800" height="220"&gt;&lt;/a&gt;&lt;br&gt;
Why didn’t this throw an error?&lt;br&gt;
Why does let behave differently? (We'll study this later in another blog)&lt;br&gt;
And why do functions work even before they’re defined?&lt;/p&gt;

&lt;p&gt;The answer to all of this lives in &lt;strong&gt;Execution Context&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Once I understood how JavaScript prepares code before running it, hoisting stopped being confusing and started making sense.&lt;/p&gt;

&lt;p&gt;Let's break it down.&lt;br&gt;
&lt;strong&gt;Hoisting happens in the creation phase&lt;/strong&gt;, not during the execution.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Hoisting?
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Hoisting is JavaScript’s behavior of moving declarations to the top of their scope during the creation phase.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Not physically moving code — but &lt;strong&gt;allocating memory before execution starts&lt;/strong&gt;.&lt;br&gt;
Let's understand hoisting for var and functions with an example:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjjo8qkf1mdcn7vhe1c4s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjjo8qkf1mdcn7vhe1c4s.png" alt=" " width="800" height="393"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;At first glance, this code looks wrong.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We are calling printMessage() before defining it.&lt;/li&gt;
&lt;li&gt;We are logging x before assigning it.&lt;/li&gt;
&lt;li&gt;We are even logging the function itself.
Yet… this code &lt;strong&gt;works&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;Now let's start with var.&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frp5zuxx02pbopyfdaelw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frp5zuxx02pbopyfdaelw.png" alt=" " width="800" height="220"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What You Expect
&lt;/h2&gt;

&lt;p&gt;Error, because &lt;strong&gt;x&lt;/strong&gt; is declared later.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Actually Happens
&lt;/h2&gt;

&lt;p&gt;JavaScript stores the variable with a special keyword &lt;strong&gt;undefined&lt;/strong&gt;.&lt;/p&gt;

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

&lt;p&gt;During the creation phase, JavaScript does this:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw9jrz7wgqbxu21405s19.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw9jrz7wgqbxu21405s19.png" alt=" " width="800" height="105"&gt;&lt;/a&gt;&lt;br&gt;
Then during execution:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffll8856f39tzd7hybfgb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffll8856f39tzd7hybfgb.png" alt=" " width="54" height="20"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Key Rules for var
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;var is &lt;strong&gt;hoisted&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Memory is allocated&lt;/li&gt;
&lt;li&gt;Initialized with undefined&lt;/li&gt;
&lt;li&gt;Scoped to &lt;strong&gt;function&lt;/strong&gt;, not block&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is why var feels “weird” — but it’s actually consistent once you understand execution context.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Functions Are Fully Hoisted
&lt;/h2&gt;

&lt;p&gt;This is where things get interesting.&lt;br&gt;
During the creation phase:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fndo25utwcofviuuy8wb7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fndo25utwcofviuuy8wb7.png" alt=" " width="800" height="324"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;JavaScript allocates memory&lt;/li&gt;
&lt;li&gt;Stores the &lt;strong&gt;entire function definition&lt;/strong&gt;
So in memory: 
&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fms9hdcrnehettkbq785g.png" alt=" " width="300" height="166"&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;JavaScript already knows what printMessage() is before execution begins. This is why function declarations are &lt;strong&gt;fully hoisted&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Now JavaScript starts executing the code from top to bottom.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Line 1&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqrfqeho3cht3vfjvqhiw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqrfqeho3cht3vfjvqhiw.png" alt=" " width="800" height="203"&gt;&lt;/a&gt;&lt;br&gt;
JavaScript finds the full printMessage() function in memory.&lt;br&gt;
Output:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0v0pk6m22doxqneuxzoe.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0v0pk6m22doxqneuxzoe.png" alt=" " width="145" height="28"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Line 2&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxpst6bndd5qahe8h6h35.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxpst6bndd5qahe8h6h35.png" alt=" " width="800" height="203"&gt;&lt;/a&gt;&lt;br&gt;
JavaScript looks for x in memory.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;x exists&lt;/li&gt;
&lt;li&gt;Its value is undefined
Output:
&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fegpuwbppru55pnnsyout.png" alt=" " width="79" height="26"&gt;
&amp;gt; Important 
&amp;gt; This is &lt;strong&gt;not an error&lt;/strong&gt; because x was already created during the creation phase.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Line 3&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwlx7pm54u9zqckunag7z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwlx7pm54u9zqckunag7z.png" alt=" " width="800" height="203"&gt;&lt;/a&gt;&lt;br&gt;
Since printMessage points to the entire function, JavaScript prints the function itself.&lt;br&gt;
Output:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7bmg8o8st0ftewz67nim.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7bmg8o8st0ftewz67nim.png" alt=" " width="260" height="53"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Line 5&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzi91x0h0l0rw0a0foa1r.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzi91x0h0l0rw0a0foa1r.png" alt=" " width="800" height="203"&gt;&lt;/a&gt;&lt;br&gt;
Now, during execution, x gets its actual value.&lt;br&gt;
Memory update: &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;x → 10&lt;br&gt;
&lt;strong&gt;Final Output&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0jitfyquh205yeil9q2z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0jitfyquh205yeil9q2z.png" alt=" " width="268" height="108"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now hoisting works little different for function expression.&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnxnrkiolbfjta4hv8su8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnxnrkiolbfjta4hv8su8.png" alt=" " width="800" height="358"&gt;&lt;/a&gt;&lt;br&gt;
TypeError: printMessage is not a function&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzse837ji186qvbtkq94v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzse837ji186qvbtkq94v.png" alt=" " width="406" height="32"&gt;&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;Why?&lt;/strong&gt;&lt;br&gt;
Because only the variable is hoisted, not the function.&lt;br&gt;
Creation phase:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp68q6ft0twt924w5xi07.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp68q6ft0twt924w5xi07.png" alt=" " width="800" height="70"&gt;&lt;/a&gt;&lt;br&gt;
This is why it throws an error. &lt;/p&gt;

&lt;p&gt;For arrow functions it works in the same way how function expression behaves.&lt;br&gt;
Finally, Hoisting is not &lt;strong&gt;magic&lt;/strong&gt;.&lt;br&gt;
It’s a &lt;strong&gt;side effect of how JavaScript creates execution contexts&lt;/strong&gt;.&lt;br&gt;
Once you understand:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Creation phase&lt;/li&gt;
&lt;li&gt;Memory allocation&lt;/li&gt;
&lt;li&gt;TDZ&lt;/li&gt;
&lt;li&gt;Scope rules&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Hoisting becomes predictable, logical, and honestly… kind of elegant.&lt;/p&gt;

&lt;p&gt;If You like my content and want to connect with me, connect me on &lt;a href="https://www.linkedin.com/in/amohanta" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>learning</category>
    </item>
    <item>
      <title>JavaScript Execution Context and Call Stack Explained</title>
      <dc:creator>Ananya Mohanta</dc:creator>
      <pubDate>Sat, 07 Feb 2026 13:55:48 +0000</pubDate>
      <link>https://dev.to/ananya_mohanta_b44a988e76/javascript-execution-context-and-call-stack-explained-3bd5</link>
      <guid>https://dev.to/ananya_mohanta_b44a988e76/javascript-execution-context-and-call-stack-explained-3bd5</guid>
      <description>&lt;h2&gt;
  
  
  Why Execution Context is important in JavaScript ?
&lt;/h2&gt;

&lt;p&gt;When I first learnt JavaScript, I tried to memorize syntax but struggled to understand why the code behaved the way it did.&lt;/p&gt;

&lt;p&gt;What are all these jargons like : &lt;strong&gt;Hoisting, Scope, Closure, async behavior ?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;All became easy once I understood Execution Context &amp;amp; Call stack.&lt;/p&gt;

&lt;p&gt;So, this is where my JavaScript learning journey begins, and from today onwards, I will be sharing my learning so that it can be easy for you all to learn these concepts.&lt;/p&gt;

&lt;p&gt;Let’s start with the very first question.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Execution Context?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;An Execution context is the environment where JavaScript code is evaluated and executed.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;What does it mean by this line? Let me explain.&lt;/p&gt;

&lt;p&gt;Think of it as a big container created by JS to run your code. Everything in JS runs inside an execution context. Every time JS runs code, it creates an execution context.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is inside an execution context?
&lt;/h2&gt;

&lt;p&gt;The execution context is created in two phases.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Memory Creation Phase&lt;/li&gt;
&lt;li&gt;Code Execution Phase&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Phase 1 - Memory Creation Phase :&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Before executing the code, JS stores variables and functions in the memory.&lt;/p&gt;

&lt;p&gt;During this phase :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In the memory component everything is stored in &lt;strong&gt;key-value&lt;/strong&gt; pairs.&lt;/li&gt;
&lt;li&gt;Variables are stored with a special keyword “&lt;strong&gt;undefined&lt;/strong&gt;“ (I will talk about it in another blog).&lt;/li&gt;
&lt;li&gt;Functions are stored as it is.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Phase 2 - Code Execution Phase :&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In this phase :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Code will be executed &lt;strong&gt;one line at a time&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Variable values are updated with the values written in the code.&lt;/li&gt;
&lt;li&gt;Functions are invoked/executed in this phase.&lt;/li&gt;
&lt;li&gt;Function calls are pushed onto the &lt;strong&gt;Call Stack&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is also known as &lt;strong&gt;Thread of Execution&lt;/strong&gt;. This is why JavaScript is called &lt;strong&gt;Synchronous Single Threaded language&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;What does it mean by Synchronous Single Threaded ?&lt;/p&gt;

&lt;p&gt;It means the code will execute one line at a time in a particular order that is, after finishing the execution of one line, the next line will executes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Types of Execution Context
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Global Execution Context :&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;It is created when JS file first runs.&lt;/li&gt;
&lt;li&gt;Only one global context exists.&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Function Execution Context :&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;It is created every time a function is called.&lt;/li&gt;
&lt;li&gt;Can be as many as functions are invoked in the code.&lt;/li&gt;
&lt;li&gt;When a function is created, a new execution context is created.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Example to Visualize Execution Context
&lt;/h2&gt;

&lt;p&gt;Let’s understand the flow of execution context with an example&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5okh2mlg0vn2svqnf5ii.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5okh2mlg0vn2svqnf5ii.png" alt=" " width="800" height="458"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the memory creation phase, variable n will be stored with undefined. and function will be stored as it is and everything is stored in key-value pairs. We can see this in developer console.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F979b63dvwj48w1roc2fo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F979b63dvwj48w1roc2fo.png" alt=" " width="338" height="184"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In code execution phase a global execution is created and variables will assigned with its value written in the code. The code will execute one line by line at a time in a particular order.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsm32e7im1xs5q0eie9tq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsm32e7im1xs5q0eie9tq.png" alt=" " width="619" height="50"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvgxjy20356uiqhevcvof.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvgxjy20356uiqhevcvof.png" alt=" " width="43" height="23"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, &lt;strong&gt;n&lt;/strong&gt; is assigned with value &lt;strong&gt;5&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Now when the 6th line is executed, the &lt;strong&gt;multiplyByTwo()&lt;/strong&gt; function is invoked with an argument &lt;strong&gt;n&lt;/strong&gt; and a new execution context is created and pushed into the Call Stack.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foeaxfi7194u1dqd6sdfm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foeaxfi7194u1dqd6sdfm.png" alt=" " width="295" height="138"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is known as Function Execution Context. Once the code execution is completed and the value is assigned into &lt;strong&gt;output1&lt;/strong&gt;, the execution context is popped from the call stack.&lt;/p&gt;

&lt;p&gt;Similarly for &lt;strong&gt;output2&lt;/strong&gt;, the same process happens.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Is the Call Stack?
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;Call Stack&lt;/strong&gt; is a data structure that keeps track of &lt;strong&gt;execution contexts&lt;/strong&gt;. It follows &lt;strong&gt;LIFO - Last In, First Out&lt;/strong&gt; principle.&lt;/p&gt;

&lt;p&gt;JavaScript has it own call stack. Call stack maintains the order of the execution of execution context. It is also known as &lt;strong&gt;Execution Context Stack / Program Stack / Central Stack / Runtime Stack / Machine Stack&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  How Call Stack Works
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F31qftnbqan2t78be08za.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F31qftnbqan2t78be08za.png" alt=" " width="800" height="593"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The order of the call stack is as below :&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Global - First, the code executes and global execution context is created.&lt;/li&gt;
&lt;li&gt;first() - After this, first() function is invoked and and execution context pushed onto call stack.&lt;/li&gt;
&lt;li&gt;second() - Then second function is invoked inside first() and and execution context pushed onto call stack.&lt;/li&gt;
&lt;li&gt;third()- Then third function is invoked inside second() and and execution context pushed onto call stack.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Once the third() finishes, it popped from call stack. Then second() and first() finish execution and popped from the call stack as well.&lt;/p&gt;

&lt;h2&gt;
  
  
  Importance
&lt;/h2&gt;

&lt;p&gt;Understanding execution context helps you:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Predict hoisting behavior&lt;/li&gt;
&lt;li&gt;Debug scope issues&lt;/li&gt;
&lt;li&gt;Understand closures&lt;/li&gt;
&lt;li&gt;Avoid call stack overflow&lt;/li&gt;
&lt;li&gt;Write better async code&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Finally, Execution Context &amp;amp; Call Stack are the &lt;strong&gt;foundation of JavaScript&lt;/strong&gt;. Once you understand this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Closures feel logical&lt;/li&gt;
&lt;li&gt;Async behavior makes sense&lt;/li&gt;
&lt;li&gt;Debugging becomes easier.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If You like my content and want to connect with me, follow me on LinkedIn(&lt;a href="https://www.linkedin.com/in/amohanta" rel="noopener noreferrer"&gt;https://www.linkedin.com/in/amohanta&lt;/a&gt;).&lt;/p&gt;

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