<?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: Sreekanth Manda</title>
    <description>The latest articles on DEV Community by Sreekanth Manda (@sreekanthmanda).</description>
    <link>https://dev.to/sreekanthmanda</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%2F72218%2Fdbaab04a-6e71-49be-a85d-851eae404dde.jpeg</url>
      <title>DEV Community: Sreekanth Manda</title>
      <link>https://dev.to/sreekanthmanda</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sreekanthmanda"/>
    <language>en</language>
    <item>
      <title>Hoisting: The most quirky term in JavaScript</title>
      <dc:creator>Sreekanth Manda</dc:creator>
      <pubDate>Tue, 17 Dec 2019 00:01:44 +0000</pubDate>
      <link>https://dev.to/sreekanthmanda/hoisting-the-most-quirky-term-in-javascript-3cp1</link>
      <guid>https://dev.to/sreekanthmanda/hoisting-the-most-quirky-term-in-javascript-3cp1</guid>
      <description>&lt;h1&gt;
  
  
  Hoisting in Javascript
&lt;/h1&gt;

&lt;p&gt;Ever wondered how the &lt;strong&gt;undefined&lt;/strong&gt; gets set to a variable when declared but not initialised or a function that gets &lt;strong&gt;executed&lt;/strong&gt; even before its declaration?&lt;/p&gt;

&lt;p&gt;Yes, I was one of them who used to think how a variable that was never initialised has a value called &lt;em&gt;undefined&lt;/em&gt; in it when I first started with Javascript.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;em&gt;Variable hoisting&lt;/em&gt;
&lt;/h4&gt;

&lt;p&gt;Let's look at a very simple example code below&lt;/p&gt;


&lt;div class="runkit-element"&gt;
  &lt;code&gt;
    
  &lt;/code&gt;
  &lt;code&gt;
    console.log(greet);

var greet;


  &lt;/code&gt;
&lt;/div&gt;
 

&lt;p&gt;this outputs &lt;em&gt;undefined&lt;/em&gt; to the console, as per MDN, undefined is a primitive value automatically assigned to variables that have just been declared. &lt;/p&gt;

&lt;p&gt;The process of assigning the primitive undefined values to declared variables is called &lt;strong&gt;hoisting&lt;/strong&gt; in Javascript.&lt;/p&gt;

&lt;p&gt;Let’s look at when exactly hoisting happens when a javascript code gets executed.&lt;/p&gt;

&lt;p&gt;Before I talk about this, I need to touch a concept called execution context in Javascript. Execution context is an abstract concept of an environment where the Javascript code is evaluated and executed in two different stages&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;creation phase&lt;/li&gt;
&lt;li&gt;execution phase (I won’t be talking about execution phase in this post)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Creation phase is where the variable and function declarations are put into memory, but stay exactly where you have written them in your code. In the creation phase, all variables that are declared but not initialised with a value will be assigned &lt;code&gt;undefined&lt;/code&gt; by javascript engine.&lt;/p&gt;

&lt;p&gt;That is how we see &lt;em&gt;undefined&lt;/em&gt; value for variables that are not initialised but used in your code.&lt;/p&gt;

&lt;p&gt;As a good practice, avoid initialising variables with &lt;strong&gt;undefined&lt;/strong&gt; in your code.&lt;/p&gt;
&lt;h4&gt;
  
  
  &lt;em&gt;Function hoisting&lt;/em&gt;
&lt;/h4&gt;

&lt;p&gt;Functions also get allocated in memory during creation phase, which will allow us to use a function before it is declared in the code. &lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;


&lt;div class="runkit-element"&gt;
  &lt;code&gt;
    
  &lt;/code&gt;
  &lt;code&gt;
    sayHello('World');

function sayHello(name) {
  console.log('Hello! ' + name);
}

/*
The result of the code above is: "Hello! World"
*/


  &lt;/code&gt;
&lt;/div&gt;


&lt;p&gt;Let's look at above code example, I called the function in my code first, even before the function is written, the code still works. This is because of execution context in JavaScript.&lt;/p&gt;

&lt;p&gt;One thing to keep in mind about hoisting is, only declarations get hoisted in JavaScript, not the initialisations.&lt;/p&gt;

&lt;p&gt;I hope this post helps to understand and clear confusion about variable and function &lt;strong&gt;hoisting&lt;/strong&gt; in JS.&lt;/p&gt;

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