Hoisting in Javascript(Part 1)
Tags: javascript, beginners, hoisting, webdev
Published: 2021-05-13 03:56:49


       Interviewer: Tell me, what is variable hoisting in JS?

       devDood:  The concept of variable and function 
                 declarations to physically moved to the top 
                 of your code is called hoisting

Is this factually right??🤔

Absolutely not!!!

If this is not the answer, then what is variable hoisting?

The main agenda of this post is to make you understand how hoisting works in Javascript. I highly recommend you to read the post on execution context before continuing further.

{% link https://dev.to/prashan81992916/how-does-js-code-run-execution-context-and-call-stack-3a7 %}



So Lets start,

In Javascript, both variables and functions are hoisted which basically means, no error is thrown when you reference a variable or a function before declaration.

__Variable hoisting__

```javascript
console.log(a);
var a = 5;
```

Take a moment to think what will be printed in the console.

If your answer is ``undefined`` then that's right, but why it is printing ``undefined`` rather than throwing some error?. If you try to reference a variable before declaration in some other language(like C,C++,...) you will get an error saying``a is not defined``. So lets find out why??.

        Note: Undefined and not defined are not same

        Undefined - It is one of the 7 primitive types in js 
                    which is initialised to all the variables 
                    during memory creation phase. It is 
                    possible to explicitly assign it to a 
                    variable(for ex a=undefined) , but good 
                    practice is not to do so, as it is used to 
                    serve a specific purpose

        not defined - In JS, it is a Reference error that we 
                      get when referencing to an undeclared 
                      variable as it is nowhere available in
                      its scope

If you have read the post on ``execution context`` , you now know that execution context consists of two phases - ``memory creation and code execution``. During memory creation phase the special keyword ``undefined`` is initialised to variables declared in the code. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ubkqzzp2oodwwlaudtj3.png)

During code execution phase when the control goes to the first line , the js engine looks for the value of ``a`` in the current ``execution context's memory`` which has the value ``undefined``.This is the reason why undefined is printed in the console.After which 5 is assigned to the variable a in the memory.If you remove the declaration statement (__let a = 5;__), then you get a reference error (__a is not defined__) as it is not available in its memory space. 

__Function hoisting__

```javascript
a();
function a(){
//Prashanth says Hi, 🙌🏻
}
```
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ph0y1g64lcp77wxebxvi.png)

Just like variables, functions are also hoisted. This is because, during memory allocation phase function code is copied as it is instead of initialising with undefined as we saw in variables. So in the code execution phase when the control hits the first line - function invocation, js engine will look in the memory, gets the function code and executes it gracefully.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5onxv2o7bkj40f9jbryc.gif)

Enough of all the boring theoretical mumbo jumbo........... 
let's try to learn using chrome dev tools.

__Learn hoisting using Dev Tools__

We know that whenever js code runs, a global execution context(global scope) is created. Trust me, this is also true even if you didn't write a single line of code.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4hrbsr7d84otgprww144.png)


```javascript
var a = 100;
var a = 200;
console.log(a);
function main() {
  console.log(a);
  var a = 400;
  {
  var a = 500;
  }
  console.log(a);
}
main();
console.log(a);
```


With all the basics that you learned above, try to figure out what will be printed in the console and write your answers with explanation in the comment section.

Voilà!!!. You have completed the part1 of hoisting.

Follow me back to know the detailed explanation of above code using dev tools - Part✌🏻 on hoisting