<?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: HadarHarush</title>
    <description>The latest articles on DEV Community by HadarHarush (@hadarharush).</description>
    <link>https://dev.to/hadarharush</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%2F615536%2F89762a5c-b32a-4c0a-9a0d-72d4822fc221.jpeg</url>
      <title>DEV Community: HadarHarush</title>
      <link>https://dev.to/hadarharush</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/hadarharush"/>
    <language>en</language>
    <item>
      <title>The difference between var and let</title>
      <dc:creator>HadarHarush</dc:creator>
      <pubDate>Fri, 16 Apr 2021 11:18:48 +0000</pubDate>
      <link>https://dev.to/hadarharush/the-difference-between-var-and-let-3p90</link>
      <guid>https://dev.to/hadarharush/the-difference-between-var-and-let-3p90</guid>
      <description>&lt;p&gt;Var and let are two varible declaration types in javascript, that have lot of common behaviours. Though, there is some big differnces between this 2, and every javascript programmer needs to know them.&lt;/p&gt;

&lt;p&gt;For understanding some of this differences, first we need to understand the difference between two javascript scopes : the &lt;strong&gt;Function scope&lt;/strong&gt;, and the &lt;strong&gt;Blocked scope&lt;/strong&gt;. &lt;strong&gt;Function scope&lt;/strong&gt; is the area between the two curly braces that comes after the declaration:&lt;br&gt;
&lt;code&gt;function func1(){&lt;br&gt;
   //we are now inside the function scope...&lt;br&gt;
}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Block scope&lt;/strong&gt; is the area between two curly braces &lt;strong&gt;that not bind to a function&lt;/strong&gt;. For example:&lt;br&gt;
&lt;code&gt;{let l1 = 'l1' //we just declared the varibale l1 inside a block scope}&lt;/code&gt;&lt;br&gt;
but also in:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if(2 &amp;gt; 1){
  let l2 = 'l2'
  console.log(l2)
  //we declared the variable l2 inside block scoped 
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both scope types have a lexical variables scope. it means that in both scopes, if we are declaring a variable inside of them, we will cannot be access this variables from an outer scope:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if(2 &amp;gt; 1){let l3 = 'l3'}
 console.log(l3) //Error, because we not recognize l3 outside of its scope
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and also:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function func2(){let l4 = 'l4'}
func2()
console.log(l4) //Error, because we not recognize l4 outside of its scope
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But, there is a little problem we didnt mention: &lt;strong&gt;In a Block scope, a var declaration will leak out!&lt;/strong&gt;&lt;br&gt;
Not like &lt;code&gt;const&lt;/code&gt; or &lt;code&gt;let&lt;/code&gt;, &lt;code&gt;var&lt;/code&gt; that declares inside  a block scope will leak out to the outer scope. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if(2 &amp;gt; 1){
  let l5 = 'l5'
  var v5 = 'v5'
}
console.log(l5) //Error, because we not recognize l5 outside of its scope
console.log(v5) //outputs 'v5'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So what happened in the code above?&lt;br&gt;
We declared the variable l5 with the keyword &lt;code&gt;let&lt;/code&gt;, and its remained inside is block scope.&lt;br&gt;
We declared the variable v5 with the keyword &lt;code&gt;var&lt;/code&gt;, and as we say before, it leaked out to the outer scope, (this case: the global scope).&lt;/p&gt;

&lt;p&gt;Notice that this behaviour occurs only inside a &lt;strong&gt;Block scope&lt;/strong&gt;.&lt;br&gt;
in &lt;strong&gt;Function scope&lt;/strong&gt;, the rules are still clear, and all the 3 keywords: &lt;code&gt;var&lt;/code&gt;, &lt;code&gt;let&lt;/code&gt;, and &lt;code&gt;const&lt;/code&gt;, will remain in the &lt;strong&gt;Function scope&lt;/strong&gt; and wont leak out:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function func3(){
  var v6 = 'v6'
  let l6 = 'l6' 
}
func3()
console.log(v6) //Error, because we not recognize v6 outside of its scope
console.log(l6) //Error, because we not recognize l6 outside of its scope
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Another difference between &lt;code&gt;var&lt;/code&gt; and &lt;code&gt;let&lt;/code&gt; is the re-declaration.&lt;br&gt;
in &lt;code&gt;var&lt;/code&gt;, a declaration of variable with a name that is taken by another variable, will just apply the new value to the variable:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var v7 = 'v7'
var v7 = 'javascript'
console.log(v7) //outputs 'javascript'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;in &lt;code&gt;let&lt;/code&gt;, it wont happend, and this operation will cause an Error:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let l7 = 'l7'
let l7 = 'l8' //Error, the variable-name "l7" is already taken
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now when we know thes 2 diffrences we can discuss a famous Error that caused by the &lt;code&gt;var&lt;/code&gt; keyword, and how we can fix it easily with the &lt;code&gt;let&lt;/code&gt; keyword:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for (var i = 0; i &amp;lt; 3; i++) {
  setTimeout(() =&amp;gt; {
    console.log(i)
  }, 1000)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output we except (after a second) is "0 1 2", but actually, the output we will get is "3 3 3" &lt;strong&gt;Why is that?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When we insert the first statement of the for loop (&lt;code&gt;var i = 0&lt;/code&gt;),&lt;br&gt;
the code that actually will be in the block of code inside will be something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  var i = 0
  setTimeout(() =&amp;gt; {
    console.log(i)
  }, 1000)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Moreover, if you noticed, this &lt;strong&gt;code-block&lt;/strong&gt; is actually a &lt;strong&gt;Block scope&lt;/strong&gt;, and its means that all the rules that we mentioned earlier are applies to here too.&lt;/p&gt;

&lt;p&gt;Therefore, in the first iteration, when the javascript engine sees a var declaration inside of &lt;strong&gt;Block scope&lt;/strong&gt;, he leaks its out to the outer scope (in this case: the gloabl scope), so now, the variable, i, will be placed in the global scope!&lt;/p&gt;

&lt;p&gt;in the second iteration, when javascript engine will read the &lt;br&gt;
&lt;code&gt;var i = 1&lt;/code&gt; inside a &lt;strong&gt;Block scope&lt;/strong&gt;, he will want to put it in the global scope again, but this time, we already got a variable named "i" in the global scope. So, as we mention above, in this case of &lt;code&gt;var&lt;/code&gt;,  "&lt;strong&gt;in &lt;code&gt;var&lt;/code&gt;, a declaration of variable with a name that is taken by another variable, will just apply the new value to the variable&lt;/strong&gt;". So now, the global variable i is equal to 1, and the same thing will happend in the next iteration and it will be with the new value 2. at the end of the last iteration, the for loop will then increase this value by 1 again (to 3), and then the term &lt;code&gt;i &amp;lt; 3&lt;/code&gt; will be false, and the iteration stream will stop.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for (var i = 0; i &amp;lt; 3; i++) {
  setTimeout(() =&amp;gt; {
    console.log(i)
  }, 1000)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, lets talk about the process that will happen second after that. The first timeout will exceed, and the order &lt;code&gt;console.log(i)&lt;/code&gt; will be executed. But, now the i variable is equal to 3 so the output of this execution will be 3. The same thing will happen in the second timeout that we created in the for loop, and also to the third, which, for summery, will output &lt;strong&gt;"3 3 3"&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So how we can fix that with &lt;code&gt;let&lt;/code&gt;? Let's see.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for (let i = 0; i &amp;lt; 3; i++) {
  setTimeout(() =&amp;gt; {
    console.log(i)
  }, 1000)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, the only change is that the declaration type is &lt;code&gt;let&lt;/code&gt;, and not &lt;code&gt;var&lt;/code&gt;. &lt;strong&gt;So how it works?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now, every iteration code block looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  let i = 0
  setTimeout(() =&amp;gt; {
    console.log(i)
  }, 1000)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the first iteration, the variable i is declared inside the &lt;strong&gt;Block scope&lt;/strong&gt;, and as we study, it will remain there and not leek to the global scope. This occurence will apply also to the secind and third iterations. But there is an inportant fact we need to notice: also the whole three variables i are in &lt;strong&gt;Block scopes&lt;/strong&gt; that have a common "level", &lt;strong&gt;each block scope is unique, and has its own environment varibales&lt;/strong&gt;. Therefore, the i that exist in the &lt;strong&gt;Block scope&lt;/strong&gt; of the first iteration, isnt the same variable that the exist in the second iteration &lt;strong&gt;Block scope&lt;/strong&gt;, etc.&lt;/p&gt;

&lt;p&gt;its means, that when the console.log(i) execution will occur, each console.log will pring the i that he knows &lt;strong&gt;in his variable environment&lt;/strong&gt; and the output will be  "1 2 3" &lt;/p&gt;

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