<?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>Forem: Muhibullah</title>
    <description>The latest articles on Forem by Muhibullah (@muhibullah).</description>
    <link>https://forem.com/muhibullah</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%2F1041619%2F81c5565f-922f-4424-8967-77386776e388.jpeg</url>
      <title>Forem: Muhibullah</title>
      <link>https://forem.com/muhibullah</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/muhibullah"/>
    <language>en</language>
    <item>
      <title>(TDZ) Temporal Dead Zone in JavaScript – Simple Explained</title>
      <dc:creator>Muhibullah</dc:creator>
      <pubDate>Thu, 09 Mar 2023 07:18:44 +0000</pubDate>
      <link>https://forem.com/muhibullah/tdz-temporal-dead-zone-in-javascript-simple-explained-29ok</link>
      <guid>https://forem.com/muhibullah/tdz-temporal-dead-zone-in-javascript-simple-explained-29ok</guid>
      <description>&lt;h2&gt;
  
  
  What is the temporal dead zone in JavaScript?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Temporal Dead Zone(TDZ)&lt;/strong&gt; is the time when the variable exists but is still uninitialized and cannot be accessed [&lt;em&gt;so if we try to access it at this time, we will get an error&lt;/em&gt;] It ends when the code declaration. it is behavior specific to let and const variables.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  // userName TDZ starts here (local scope block)
  // userName TDZ running...
  // userName TDZ running...

  console.log(userName);
 /* ReferenceError: can't access lexical declaration 'userName' before initialization, because userName TDZ running here.*/

  // userName TDZ running...
  // userName TDZ running...

  let userName = "muhibullah"; // userName TDZ ends

  // userName TDZ not exist here
  // userName TDZ not exist here
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How does "var" TDZ different from "let" and "const" variables?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;var&lt;/strong&gt; is function scoped and can be reassigned multiple times, &lt;strong&gt;let&lt;/strong&gt; is block scoped and can be redeclared and reassigned, and &lt;strong&gt;const&lt;/strong&gt; is block scoped and cannot be reassigned after initialization. TDZ is behavior specific to &lt;strong&gt;let&lt;/strong&gt; and &lt;strong&gt;const&lt;/strong&gt; variables that prevent their use before they have been properly initialized.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  // userName TDZ start and end here.

  console.log(userName); // log: "undefined" because userName TDZ not exist.

  var userName = "muhibullah"; // userName TDZ not exist

  console.log(userName); // log: "muhibullah" because userName TDZ not exist

  // userName TDZ not exist here
  // userName TDZ not exist here
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;In summary,&lt;/strong&gt; TDZ (Temporal Dead Zone) is a behavior specific to let and const variables in JavaScript that prevents their use before they have been properly initialized. When a let or const variable is declared, it is in the TDZ until it is initialized, and trying to access it before initialization will result in a ReferenceError. This behavior is in place to prevent the use of variables before they have been properly defined.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;&lt;strong&gt;declaring and initialization&lt;/strong&gt;: declaration is the process of creating a new variable with a specific name, while initialization is the process of assigning an initial value to a variable. declaration and initialization can occur in the same statement.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>tdz</category>
    </item>
  </channel>
</rss>
