<?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: Sujit Singh</title>
    <description>The latest articles on DEV Community by Sujit Singh (@sujit510).</description>
    <link>https://dev.to/sujit510</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%2F724122%2F3901f73e-b07f-4536-a42e-1f997ce1f117.jpeg</url>
      <title>DEV Community: Sujit Singh</title>
      <link>https://dev.to/sujit510</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sujit510"/>
    <language>en</language>
    <item>
      <title>Hoisting with var</title>
      <dc:creator>Sujit Singh</dc:creator>
      <pubDate>Thu, 21 Apr 2022 06:20:51 +0000</pubDate>
      <link>https://dev.to/sujit510/hoisting-with-var-1o5e</link>
      <guid>https://dev.to/sujit510/hoisting-with-var-1o5e</guid>
      <description>&lt;p&gt;I came across a lot of articles describing hoisting in Javascript.. where in a nutshell, most of them indicating it as a feature where variable declarations are moved to the top of their scope, and few of them even illustrating with appropriate examples with var, let and const.&lt;/p&gt;

&lt;p&gt;With const and let having block level scope, it becomes easy to describe.. but I find var a bit tricky to crack hoisting with. Specially when I came across a problem faced by one of my colleague in one of the interviews.&lt;/p&gt;

&lt;p&gt;So the problem is as follows whose output was asked to explain:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var num = 4;
function test() {
    var newNum = num;
    console.log('num outside loop:', num);
    for (var num = 0; num &amp;lt; newNum; num++) {
        console.log('num inside loop:', num);
    }
}
test();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From first look, one can say it will be printing following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;num outside loop: 4
num inside loop: 0
num inside loop: 1
num inside loop: 2
num inside loop: 3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But actually it prints:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;num outside loop: undefined

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And thats it.. it doesn't even enters the loop.&lt;br&gt;
Why so? Javascript hoisting kicks in and moves the &lt;strong&gt;num&lt;/strong&gt; declaration at the top of the function &lt;strong&gt;test&lt;/strong&gt;, so effectively it becomes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var num; // hoisting
num = 4;
function test() {
    var num; // hoisting - redefines num as undefined
    var newNum;
    newNum = num; // undefined is assigned to newNum
    console.log('num outside loop:', num); // prints message followed by undefined
    for (num = 0; num &amp;lt; newNum; num++) { // var num declaration moved to top as a part of hoisting
        console.log('num inside loop:', num);
    }
}
test();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It's clear that it will never execute the loop with new value of num i.e. 0 being compared to undefined value for newNum.&lt;/p&gt;

&lt;p&gt;Thanks for reading through. Feel free to suggest corrections / additions if any.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>hoisting</category>
      <category>var</category>
    </item>
  </channel>
</rss>
