<?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: mahammadmansur95</title>
    <description>The latest articles on DEV Community by mahammadmansur95 (@mahammadmansur95).</description>
    <link>https://dev.to/mahammadmansur95</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%2F419486%2F52d2bafe-662f-46d9-82e3-a7b8036269fa.jpg</url>
      <title>DEV Community: mahammadmansur95</title>
      <link>https://dev.to/mahammadmansur95</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mahammadmansur95"/>
    <language>en</language>
    <item>
      <title>Js interview #1 : var, let, and const in JavaScript – What's the Difference?</title>
      <dc:creator>mahammadmansur95</dc:creator>
      <pubDate>Tue, 10 Jun 2025 16:54:43 +0000</pubDate>
      <link>https://dev.to/mahammadmansur95/js-interview-1-var-let-and-const-in-javascript-whats-the-difference-nhj</link>
      <guid>https://dev.to/mahammadmansur95/js-interview-1-var-let-and-const-in-javascript-whats-the-difference-nhj</guid>
      <description>&lt;p&gt;In JavaScript, variables can be declared using var, let, or const. Though they all serve the same fundamental purpose—declaring variables—they behave quite differently in terms of scope, hoisting, reassignment, and more.&lt;/p&gt;

&lt;p&gt;Let’s break down their key differences.&lt;/p&gt;

&lt;h2&gt;
  
  
  🔁 1. Scope: Global, Function, and Block
&lt;/h2&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;Scope type&lt;/td&gt;
&lt;td&gt;Function-scoped&lt;/td&gt;
&lt;td&gt;Block-scoped&lt;/td&gt;
&lt;td&gt;Block-scoped&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  👉 &lt;code&gt;var&lt;/code&gt; – Function Scope
&lt;/h3&gt;

&lt;p&gt;Variables declared with &lt;code&gt;var&lt;/code&gt; are function-scoped, which means they are only accessible inside the function in which they are defined.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function example() {
  if (true) {
    var x = 10;
  }
  console.log(x); // ✅ 10 - accessible because var is function-scoped
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  👉 &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; – Block Scope
&lt;/h3&gt;

&lt;p&gt;Both &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; are block-scoped, meaning they only exist within the enclosing &lt;code&gt;{}&lt;/code&gt; block.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function example() {
  if (true) {
    let y = 20;
    const z = 30;
  }
  console.log(y); // ❌ ReferenceError
  console.log(z); // ❌ ReferenceError
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  📦 2. Hoisting Behavior
&lt;/h2&gt;

&lt;h3&gt;
  
  
  👉 &lt;code&gt;var&lt;/code&gt; is hoisted and initialized.
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(a); // ✅ undefined
var a = 5;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  👉 &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; are hoisted but not initialized
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(b); // ❌ ReferenceError (TDZ)
let b = 10;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  🔁 3. Redeclaration &amp;amp; Reassignment
&lt;/h2&gt;

&lt;h3&gt;
  
  
  👉 &lt;code&gt;var&lt;/code&gt; allows redeclaration and reassignment
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var x = 1;
var x = 2; // ✅ No error
x = 3;     // ✅ No error
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  👉 &lt;code&gt;let&lt;/code&gt; allows reassignment but not redeclaration
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let y = 1;
let y = 2; // ❌ SyntaxError
y = 3;     // ✅ Allowed
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  👉 &lt;code&gt;const&lt;/code&gt; allows neither
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const z = 1;
const z = 2; // ❌ SyntaxError
z = 3;       // ❌ TypeError
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  ⛔ 4. Temporal Dead Zone (TDZ)
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;The Temporal Dead Zone (TDZ)&lt;/strong&gt; is the period between entering the scope and the actual declaration where variables declared with let and const cannot be accessed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function test() {
  console.log(a); // ❌ ReferenceError (TDZ)
  let a = 10;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With &lt;code&gt;var&lt;/code&gt;, accessing the variable before the declaration just returns &lt;code&gt;undefined&lt;/code&gt;, but with &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt;, it throws an error due to the TDZ.&lt;/p&gt;

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