<?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: Nathan Cotrim Lemos</title>
    <description>The latest articles on DEV Community by Nathan Cotrim Lemos (@nate_sfteng).</description>
    <link>https://dev.to/nate_sfteng</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%2F779805%2F599c119a-9a07-47b0-94b8-6cbd14a63cb4.jpeg</url>
      <title>DEV Community: Nathan Cotrim Lemos</title>
      <link>https://dev.to/nate_sfteng</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nate_sfteng"/>
    <language>en</language>
    <item>
      <title>Javascript Hoisting</title>
      <dc:creator>Nathan Cotrim Lemos</dc:creator>
      <pubDate>Sun, 02 Jan 2022 16:34:16 +0000</pubDate>
      <link>https://dev.to/nate_sfteng/javascript-hoisting-5192</link>
      <guid>https://dev.to/nate_sfteng/javascript-hoisting-5192</guid>
      <description>&lt;h2&gt;
  
  
  What is
&lt;/h2&gt;

&lt;p&gt;Hoisting is basically a behavior from javascript that we can see in execution time, it makes functions and variables go to the top of the scope in which they were declared.&lt;/p&gt;




&lt;h2&gt;
  
  
  Const, let &amp;amp; var
&lt;/h2&gt;

&lt;p&gt;Before see some examples, we need to understand the difference between these three ways to declare a variable.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;var&lt;/strong&gt;: On the start of javascript, we had only var, currently, we don't have reasons to use it, the hoisting concept is applied just to this type of declaration, and, it can be problematic by some reasons I will explain later. Another point we can say here is, its value can be reassigned.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;let&lt;/strong&gt;: unlike var, hoisting is not applied to let, it's a reason to prefer it, but, like var, its value can be reassigned.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;const&lt;/strong&gt;: like let, hoisting is not applied to it, another point about const is that it can't has its value reassigned.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;strong&gt;to stay tuned&lt;/strong&gt;: in this case, reassigned means that variable can or not has a new value, don't think that it applies to object and arrays manipulation, we can do this normally.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;resume&lt;/strong&gt;: with const and let among us, prefer use they, when you will need to change the value from any variable in execution time, prefer let, unlike it, prefer const.&lt;/p&gt;




&lt;h2&gt;
  
  
  Operation
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Functions&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;hoistMe() // output: I'm being executed by hoisting

function hoistMe() {
   console.log('I\'m being executed by hoisting')
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the example above, the hoisting works effective, repair we call the function before its initialization!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Variables&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In the example below, we use a &lt;em&gt;const&lt;/em&gt;, referring to it, we repair that hoisting is not applyied!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(assignedToConst) // output: cannot access before initialization

const assignedToConst = 'hoisted'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;OBS: with let the result would be the same&lt;/p&gt;




&lt;p&gt;Now we can see a interesting behavior from hoisting&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(assignedToVar) // output: cannot access before initialization

const assignedToVar = 'hoisted'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;executing this piece of code above, we see the specified output, it occurs because hoisting not assign the value, just declares on top, the assignment occurs where it was really implemented! Pay attention with this behavior, it makes your code ready to bugs!&lt;/p&gt;




&lt;p&gt;Unusual use case but worth understanding:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;handle = '\'hoisted\'';

console.log(handle); // output: 'hoisted'

var handle;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;if we assign the value before and declare it later the hoisting is done successfully&lt;/p&gt;




&lt;h2&gt;
  
  
  Considerations
&lt;/h2&gt;

&lt;p&gt;This explained concept works independent the scope, still the presented examples be all at global 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;function anotherHoistTest() {
   hoistMe()
   function hoistMe() {
      console.log('hoisted');
   }
}
anotherHoistTest() // output: hoisted
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;I think is good say to you do not enjoy from this behavior, it makes your code more fragile and less readable and concise.&lt;/p&gt;




&lt;h2&gt;
  
  
  Author
&lt;/h2&gt;

&lt;p&gt;Follow me:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://linktr.ee/nate.sfteng"&gt;https://linktr.ee/nate.sfteng&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

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