<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Hemant Kaushik on Medium]]></title>
        <description><![CDATA[Stories by Hemant Kaushik on Medium]]></description>
        <link>https://medium.com/@kaushikhemant166?source=rss-3dbb30822fd9------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*xKUkNkUYkxcf0p8R</url>
            <title>Stories by Hemant Kaushik on Medium</title>
            <link>https://medium.com/@kaushikhemant166?source=rss-3dbb30822fd9------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 16:38:29 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@kaushikhemant166/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[“You Thought You Knew Hoisting? Meet the Temporal Dead Zone”]]></title>
            <link>https://medium.com/@kaushikhemant166/you-thought-you-knew-hoisting-meet-the-temporal-dead-zone-806bb6751cc4?source=rss-3dbb30822fd9------2</link>
            <guid isPermaLink="false">https://medium.com/p/806bb6751cc4</guid>
            <category><![CDATA[temporal-dead-zone]]></category>
            <category><![CDATA[hoisting-and-tdz-in-js]]></category>
            <category><![CDATA[variable-hoisting]]></category>
            <category><![CDATA[uncaught-referenceerror]]></category>
            <category><![CDATA[hoisting]]></category>
            <dc:creator><![CDATA[Hemant Kaushik]]></dc:creator>
            <pubDate>Fri, 25 Jul 2025 11:35:21 GMT</pubDate>
            <atom:updated>2025-07-25T11:35:21.642Z</atom:updated>
            <content:encoded><![CDATA[<p><em>Ever seen this weird error in JavaScript:<br> </em><em>ReferenceError: Cannot access &#39;x&#39; before initialization<br> Even though you declared the variable?</em></p><p><em>Don’t worry, it’s not magic — it’s the </em><strong><em>Temporal Dead Zone (TDZ)</em></strong><em> at play, combined with </em><strong><em>Hoisting</em></strong><em>.</em></p><p>In this blog, let’s break down what hoisting means and what the TDZ is.</p><h3><strong>What is Hoisting?</strong></h3><p><strong>Hoisting</strong> means JavaScript <strong>moves variable, function declarations or class definitions to the top of their scope</strong> before executing code.</p><p>But — <em>only the declarations</em>, not the actual values.</p><p>There are <strong><em>two phases</em></strong> in the life cycle of hoisting — Creation Phase, Execution Phase.</p><ul><li>At first, <strong>Javascript engine</strong> scan the whole codebase during the <strong>creation phase</strong>. It recognizes the variables, functions, or class in the code and allocates memory for it.</li><li>Then comes the <strong>Execution Phase</strong>, where the code actually starts running line-by-line. Thanks to hoisting, variables and functions that were declared earlier are now <strong>available in memory</strong>, even if they appear later in the code.</li></ul><p>Let’s deep dive into variable hoisting :</p><h4><strong>1. Variable hoisting :</strong></h4><p>Simple Example:</p><pre>console.log(greeting);  // undefined<br>var greeting = &quot;Hello!&quot;;</pre><p><strong>Why no error?</strong><br> Because this is what happens behind the scenes:</p><pre>var greeting; // hoisted<br>console.log(greeting); // undefined<br>greeting = &quot;Hello!&quot;;</pre><p>var is hoisted and initialized with undefined.</p><p><strong>var vs </strong><strong>let vs </strong><strong>const — What&#39;s Really Happening?</strong></p><p><strong>Hoisting in <em>“Var” : <br></em></strong><em>example :</em></p><pre>var a = 5;</pre><p>Due to hoisting :</p><pre>var a; // Declaration<br>a= undefined; // intialization<br>a = 5; // assignment</pre><p>in <strong><em>“Var”</em></strong> hoisting the declaration and initialization of variables comes in <strong>creation phase</strong> while the assignment part falls in <strong>execution phase</strong>.</p><p><strong>Hoisting in <em>“Let” : <br></em></strong><em>example :</em></p><pre>let a = 5;</pre><p>Due to hoisting :</p><pre>let a; // Declaration<br>a= undefined; // intialization<br>a = 5; // assignment</pre><p>in <strong><em>“Let”</em></strong> hoisting only the declaration part of variables comes in <strong>creation phase</strong> while the initialization and assignment part falls in <strong>execution phase</strong>.</p><p><strong>Hoisting in <em>“Const” : <br></em></strong><em>example :</em></p><pre>cont a = 5;</pre><p>Due to hoisting :</p><pre>const  a; // Declaration<br>a= 5; // intialization + assignment</pre><p>in <strong><em>“const”</em></strong> hoisting only the declaration part of variables comes in <strong>creation phase</strong> while the initialization and assignment part will happens simultaneously and falls in <strong>execution phase</strong>.</p><h4><strong>2. Function hoisting :</strong></h4><p>example:</p><pre>console.log(add(2, 3)); // function hoist<br>function add(a, b) {<br>   return a+ b;<br>}</pre><p>How Javascript see this code :</p><pre>function add(a, b) {<br>  return a+ b;<br>}<br>console.log(add(2, 3)); // output : 5</pre><p><strong><em>Note:</em></strong> Hoisting never works for function expressions. It only works for function declarations.</p><h3>Enter the Temporal Dead Zone (TDZ)</h3><p>It is the area of block where the variable is inaccessible until the moment when we completely initalizes its value.</p><p>The <strong>TDZ</strong> is the time between when a variable is <strong>hoisted</strong> and when it’s <strong>initialized</strong>.</p><p>If you try to access it during this time, boom 💥 — ReferenceError.</p><p><strong>TDZ applies to: </strong><strong>let and </strong><strong>const only (not </strong><strong>var)</strong></p><h4><strong>Scope of TDZ :</strong></h4><p>TDZ starts at the beginning of block’s local scope and ends at where we initialize the variable</p><p>example:</p><pre>{<br>  // TDZ starts here<br>  // TDZ continues<br>  console.log(a); // give the reference error<br>  // TDZ continues<br>  let a = 5; // TDZ ends<br>}</pre><h4>How “Var” TDZ differs from “Let” and “Const’ ?</h4><p>The main difference <em>is where the TDZ ends</em>.</p><ul><li>When Javascript hoists a <strong><em>“Var” </em></strong>variable it automatically intializes the variable with undefined value.</li><li>But in “<strong><em>Let</em></strong>” and “<strong><em>Const”</em></strong> Javascript doesn’t initializes any value instead they remains dead and inaccessible.</li><li>Therefore, “<strong><em>Let</em></strong>” or “<strong><em>Const</em></strong>” TDZ ends when Javascript fully initializes it with the value specified during its declaration.</li><li>But, a <strong><em>“Var”</em></strong> TDZ ends immediately after it’s hoisting.</li></ul><h3>Conclusion</h3><p><strong>Hoisting</strong> is JavaScript trying to help you by lifting declarations to the top. But with let and const, you get <strong>TDZ</strong> as a safety net to prevent uninitialized access.</p><p>Just remember:</p><ul><li>var is hoisted and initialized (undefined)</li><li>let and const are hoisted but uninitialized → TDZ until their line</li><li>Don’t touch them before they’re declared!</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=806bb6751cc4" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>