<?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: Austin Asoluka</title>
    <description>The latest articles on Forem by Austin Asoluka (@sleekcodes).</description>
    <link>https://forem.com/sleekcodes</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%2F241435%2Fad1eff25-441d-43ac-b862-0ca0d7923035.JPG</url>
      <title>Forem: Austin Asoluka</title>
      <link>https://forem.com/sleekcodes</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/sleekcodes"/>
    <language>en</language>
    <item>
      <title>JavaScript: What exactly is Hoisting?</title>
      <dc:creator>Austin Asoluka</dc:creator>
      <pubDate>Fri, 05 May 2023 02:17:37 +0000</pubDate>
      <link>https://forem.com/sleekcodes/what-exactly-is-hoisting-3e8g</link>
      <guid>https://forem.com/sleekcodes/what-exactly-is-hoisting-3e8g</guid>
      <description>&lt;p&gt;Assume you are talking with a friend and they ask you about something that you know nothing about. Your response would be "I do not know about that".&lt;/p&gt;

&lt;p&gt;In the same way, during execution, our program needs to know about the variables we refer to or else we &lt;strong&gt;&lt;em&gt;might&lt;/em&gt;&lt;/strong&gt; get an error.&lt;/p&gt;

&lt;p&gt;Let's consider two simple code snippets to illustrate how JavaScript executes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;SNIPPET 1:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// line 1&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// line 2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Code Interpretation:&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Line 1&lt;/strong&gt;: Create a variable called &lt;code&gt;age&lt;/code&gt; and give it the value &lt;code&gt;12&lt;/code&gt;.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Line 2&lt;/strong&gt;: Get the value of &lt;code&gt;age&lt;/code&gt; and log it to the console.&lt;/p&gt;

&lt;p&gt;Notice that in the code snipped above, &lt;code&gt;var age = 12&lt;/code&gt; is written before &lt;code&gt;console.log(age)&lt;/code&gt;. JavaScript code executes from top to bottom so it is only reasonable that when &lt;strong&gt;line 2&lt;/strong&gt; executes - because we have the variable &lt;code&gt;age&lt;/code&gt; declared in &lt;strong&gt;line 1&lt;/strong&gt; before its usage in &lt;strong&gt;line 2&lt;/strong&gt; - there should be no error because having gone through &lt;strong&gt;line 1&lt;/strong&gt;, the JS engine understands that &lt;strong&gt;line 2&lt;/strong&gt; is simply requesting to log the value of &lt;code&gt;age&lt;/code&gt; which is declared in &lt;strong&gt;line 1&lt;/strong&gt; to the console.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;SNIPPET 2:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//line 1&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;//line 2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Code Interpretation:&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Line 1&lt;/strong&gt;: Get the value of &lt;code&gt;age&lt;/code&gt; and log it to the console.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Line 2&lt;/strong&gt;: Create a variable called &lt;code&gt;age&lt;/code&gt; and give it the value &lt;code&gt;12&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;When we run this program (logically speaking), we would expect to get an error because, at &lt;strong&gt;line 1&lt;/strong&gt;, we are already asking for something the compiler doesn't know about yet because it hasn't gotten to &lt;strong&gt;line 2&lt;/strong&gt; in its &lt;strong&gt;execution&lt;/strong&gt;, but against our logical reasoning, we will discover that this code will run without an error. This code runs without an error because of the concept of &lt;strong&gt;hoisting&lt;/strong&gt; and how variables created with the &lt;code&gt;var&lt;/code&gt; keyword behave.&lt;/p&gt;

&lt;p&gt;As our usual practice, let's seek to understand the term "hoisting" by examining the word used to coin it.&lt;/p&gt;

&lt;p&gt;The construct of the word tells us that it refers to an action taking place but what is the name of the action?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Hoist&lt;/strong&gt;!&lt;/p&gt;

&lt;p&gt;To hoist means to raise something. Hoisting is the process of doing just that 😉&lt;/p&gt;

&lt;p&gt;Hoisting can be loosely described as the process of raising variables to the top of their context before the context execution so that every code using (or referring to) that variable will have access to it irrespective of the line in which the code exists in the context. That is why the code written in SNIPPET 2 above could run. This is not something to celebrate until you have sufficient knowledge of what's going on behind the scene.&lt;/p&gt;

&lt;p&gt;In JavaScript, the process of hoisting happens during the creation phase of an execution context.&lt;/p&gt;

&lt;p&gt;Do you recall from &lt;a href="https://sleekcodes.hashnode.dev/javascript-execution-context-demystified"&gt;this article&lt;/a&gt; that during the creation phase of an execution context, all variable and function declarations are put in memory and assigned a value of &lt;code&gt;undefined&lt;/code&gt;? Let me explain what that "memory" is all about.&lt;/p&gt;

&lt;p&gt;The memory that is created during the creation phase of an execution context is called a &lt;strong&gt;variable object&lt;/strong&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In the Global Execution context, it is the &lt;strong&gt;Global&lt;/strong&gt; and/or &lt;strong&gt;Script&lt;/strong&gt; object. While in the function execution context, it is commonly referred to as &lt;strong&gt;Activation Object&lt;/strong&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let's put all this knowledge so far together;&lt;br&gt;&lt;br&gt;
Using &lt;strong&gt;SNIPPET 2&lt;/strong&gt; as a case study, when our program runs, during the creation phase of the global execution context, a memory is created and then the variable &lt;code&gt;age&lt;/code&gt; is attached to it with an initial value of &lt;code&gt;undefined&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Remember that in the creation phase of an execution context, all variable and function declarations are &lt;strong&gt;&lt;em&gt;taken note of&lt;/em&gt;&lt;/strong&gt;. We can dare to say they are &lt;strong&gt;hoisted&lt;/strong&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--TvKJvhqY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1682649102633/c64e33d2-3b58-4c3d-9198-b659c75960ef.png%2520align%3D%2522left%2522" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--TvKJvhqY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1682649102633/c64e33d2-3b58-4c3d-9198-b659c75960ef.png%2520align%3D%2522left%2522" alt="" width="" height=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The consequence of this phase is that the program now knows about the variable &lt;code&gt;age&lt;/code&gt;. Therefore, when the execution phase begins and at &lt;strong&gt;line 1&lt;/strong&gt; we attempt to access the value of &lt;code&gt;age&lt;/code&gt;, we'll get &lt;code&gt;undefined&lt;/code&gt; because at that point in the execution phase of the program, the variable has not been assigned a value yet. It merely just exists in memory with a default value.&lt;/p&gt;

&lt;p&gt;After line 2, if we try to access the variable again, we'll now get its actual value.&lt;/p&gt;

&lt;p&gt;So conceptually, during the creation phase of an execution context, all variable and function declarations are raised to the top of the context so that they are accessible from any line during the execution phase of that context.&lt;/p&gt;

&lt;p&gt;There are several ways to create variables in javascript using the keywords &lt;code&gt;var&lt;/code&gt;, &lt;code&gt;let&lt;/code&gt;, and &lt;code&gt;const&lt;/code&gt;. In the next section of this article, let's examine how these keywords affect the accessibility of the variables they are used to create.&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;code&gt;Var&lt;/code&gt;, &lt;code&gt;Let&lt;/code&gt; and &lt;code&gt;Const&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;var&lt;/code&gt;, &lt;code&gt;let&lt;/code&gt;, and &lt;code&gt;const&lt;/code&gt; keywords are used to declare variables in JavaScript.&lt;/p&gt;

&lt;p&gt;There are some persons with the idea that variables are only hoisted when created with the &lt;code&gt;var&lt;/code&gt; keyword (fair point) but I'd like to state that all keywords support hoisting.  &lt;/p&gt;

&lt;p&gt;The way I think about it is that the keyword we choose to use when creating variables simply affects how they are accessed in the program.&lt;/p&gt;

&lt;p&gt;One major difference between the behaviour of variables created with &lt;code&gt;var&lt;/code&gt; and those created with &lt;code&gt;let&lt;/code&gt; or &lt;code&gt;const&lt;/code&gt;; is that accessing variables created using the &lt;code&gt;let&lt;/code&gt; or &lt;code&gt;const&lt;/code&gt; keyword can only be done after the actual line where they were declared physically in the code. That is, you cannot access variables created with the &lt;code&gt;let&lt;/code&gt; or &lt;code&gt;const&lt;/code&gt; keyword before they are declared.  &lt;/p&gt;

&lt;p&gt;&lt;em&gt;The code in the snippet below will result in an error.&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// ReferenceError&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Why do I disagree with the idea that variables created using the &lt;code&gt;let&lt;/code&gt; or &lt;code&gt;const&lt;/code&gt; keyword isn't hoisted?&lt;/p&gt;

&lt;p&gt;This is because, during the creation phase of the context where they exist, they are still put in memory and assigned a value of &lt;code&gt;undefined&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;That memory is logically located at the top-most of its context making them accessible by child-contexts and child-scopes.  &lt;/p&gt;

&lt;p&gt;In the global context, a variable created with the &lt;code&gt;let&lt;/code&gt; or &lt;code&gt;const&lt;/code&gt; keyword is stored in a type of variable object called &lt;strong&gt;Script&lt;/strong&gt;. Those created with the &lt;code&gt;var&lt;/code&gt; keyword are stored in a variable object called &lt;strong&gt;Global&lt;/strong&gt;. While in the function context, all variables are stored in the functions' &lt;strong&gt;Activation Object&lt;/strong&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;NB: Do not get too concerned about the meaning of the term variable or activation object. They are simply conceptual terms to refer to a place in a context where variables created in that context are stored.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;According to &lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/Hoisting"&gt;MDN docs&lt;/a&gt;, any of the following behaviours can be regarded as hoisting;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Being able to use a variable's value in its scope before the line where it is declared.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Being able to reference a variable in its scope before the line where it is declared, without throwing a &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ReferenceError"&gt;&lt;code&gt;ReferenceError&lt;/code&gt;&lt;/a&gt;, but the value is always &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/undefined"&gt;&lt;code&gt;undefined&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The declaration of the variable causes behaviour changes in its scope before the line in which it is declared.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;All the above-stated behaviours are obtainable at the creation phase of a context.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Summary
&lt;/h3&gt;

&lt;p&gt;People mostly associate the term &lt;code&gt;hosting&lt;/code&gt; to the &lt;code&gt;var&lt;/code&gt; keyword but the truth is that even variables created using the var keyword are not listed in the &lt;a href="https://tc39.es/ecma262/#prod-HoistableDeclaration"&gt;ECMAScript Specification&lt;/a&gt; as hoistable declarations.&lt;/p&gt;

&lt;p&gt;Hoisting is just an adopted term to describe what happens during the creation phase of an execution context when variables are put in memory and assigned a default value of &lt;code&gt;undefined&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Variables created with the &lt;code&gt;var&lt;/code&gt; keyword can be referenced from any line even before the line where they are defined, but trying to reference variables created using the &lt;code&gt;let&lt;/code&gt; or &lt;code&gt;const&lt;/code&gt; keyword before the line where they are physically declared in the program will result in a &lt;code&gt;referenceError.&lt;/code&gt;  &lt;/p&gt;

&lt;p&gt;In my next article, I talk about scoping in JavaScript and explain (in detail) the effect of using the &lt;code&gt;var&lt;/code&gt;, &lt;code&gt;let&lt;/code&gt; or &lt;code&gt;const&lt;/code&gt; keyword to create variables in your program. It will help you make more informed decisions on which keyword to use when writing your programs.  &lt;/p&gt;

&lt;p&gt;&lt;em&gt;If this article was helpful to you in any way, kindly like and share (you can like up to 10 times). I'll be glad to engage your questions in the comment section&lt;/em&gt; ❤️&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>fundamentals</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
