Understanding Hoisting
Tags: webdev, javascript, codenewbie, hoisting
Published: 2020-07-02 11:04:18

## Introduction

Before we talk about hoisting, I think it is important to understand how the JavaScript engine looks at the code, interprets, and runs it. Once we understand this, hoisting becomes straightforward to explain.

## Execution Context

JavaScript engine breaks up the code into smaller segments to manage the complexity of interpreting and running it. If I have to explain this easier, think about a scenario where you attempt to write a web application. Your application typically would comprise of modules, functions, variable declarations, etc. Essentially, you have broken down the application into logical pieces in order to understand, maintain, and debug it.

Just like how modules, functions, etc. allow you to manage program complexity, Execution Context is the JavaScript engine's way to manage the complexity of interpreting the code. Hope makes things a little clearer.

### Global Execution Context

The first execution context that gets created when the JavaScript engine runs your code is called the “Global Execution Context”. Initially, this Execution Context will consist of two things - a global object and a variable called 'this'.

![Execution Context](https://dev-to-uploads.s3.amazonaws.com/i/6x235t57veklvzeb335j.png)

The above image represents the global execution in the most basic form. The 'this' keyword references the global object which is the 'window' object.

### Creation & Execution Phases

Now that we understand the global execution context, let us understand the two phases that exist while running any JavaScript program.

Let us consider the following code example:

```jsx
var fruit = apple;

function getFruit() {
	return fruit;
}
```

#### Creation Phase

The below diagram depicts how the Global Execution Context looks during the creation phase.

![Creation Phase](https://dev-to-uploads.s3.amazonaws.com/i/nsda0kz15oa6o0q2q56q.png)

In the Global `Creation` phase, the JavaScript engine will:

1. Create a global object.
2. Create an object called “this”.
3. Set up memory space for variables and functions.
4. Assign variable declarations a default value of “undefined” while placing any function declarations in memory.

#### Execution Phase

The below diagram depicts how the Global Execution Context looks during the execution phase.

![Execution Phase](https://dev-to-uploads.s3.amazonaws.com/i/do5sic88x8iypp0yv55w.png)

In the Global `Execution` Phase, the JavaScript engine will:

1. Starts running the code line by line.
2. Assigns the 'real' values to the variables already present in the memory.

Now that we've understood the creation & execution phases, let us take another example and look at the output on the console.

```jsx
console.log(`The fruit is ${fruit}`);
console.log(`The color is ${color}`);

var fruit = 'apple';
var color = 'red';

function getFruit() {
    return fruit;
}

function getColor() {
    return color;
}

//Output
//The fruit is undefined
//The color is undefined
```

Things to note:

- During the creation phase, the variables 'fruit' & 'color' are initialized with the values 'undefined'.
- Hence when the console.log statement is encountered, the value 'undefined' is printed on the console.

## Hoisting

The process of assigning variable declarations a default value of 'undefined' during the creation phase is called Hoisting.

The thing that’s confusing about “hoisting” is that nothing is actually “hoisted” or moved around. A lot of other explanations out there talk about how the code variables and functions are moved up the stack before execution without clearly talking about the creation and execution phases in the execution context.

A quick example of the below code will make sense after understanding hoisting.

```jsx
//The Variable x is initialized
x = 5;

//Output the value of x multiplied by 2 on the console
console.log(x * 2);

//The variable x is declared over here
var x;

//Output -> 10
```

In the above code example, you would notice that the variable 'x' is initialized in the first statement and then in the last statement there's a declaration of x. But, when JavaScript engine is in the creation phase, it moves up the declaration statement to the top of the stack and hence the above program would look like below when the JavaScript engine runs it.

```jsx
//The variable x is declared over here
var x;

//The Variable x is initialized
x = 5;

//Output the value of x multiplied by 2 on the console
console.log(x * 2);

//Output -> 10
```

Ideally, I would have expected the JavaScript engine to throw an error for using a variable before it has been declared, but thanks to ES6, this problem has been addressed with let & const. You can learn more about let & const [over here](https://dev.to/skaytech/the-war-var-vs-let-const-2a7l).

## What is not Hoisted?

We say that the variable declarations using let & const are not hoisted. In addition, the following are also not hoisted by the JavaScript engine:

- Functions defined with an expression.
- Arrow Functions.

## Conclusion

Hoisting is nothing but the process of assigning variable declarations a default value of 'undefined' during the creation phase.

I would suggest that now that you immediately jump to my article on Closures since it's a continuation of the concepts described in this article. You can read it [over here](https://dev.to/skaytech/understanding-closures-1501).

I hope you enjoyed this article. Do let me know your feedback and do not share it with your friends.

You may also be interested in:

- [Understanding Closures](https://dev.to/skaytech/understanding-closures-1501)
- [JavaScript Objects](https://dev.to/skaytech/all-about-javascript-functions-1l5f)
- [The WAR - var vs let vs const](https://dev.to/skaytech/the-war-var-vs-let-const-2a7l)
- [When to use setTimeout vs setInterval](https://dev.to/skaytech/when-to-use-settimeout-vs-setinterval-1mfh)
- [Module & Revealing Design Patterns](https://dev.to/skaytech/js-design-patterns-module-revealing-module-pattern-30c6)
- [History of ECMA/ES6/ES2015](https://dev.to/skaytech/history-of-ecma-es5-es6-beyond-lpe)