An Interest In:
Web News this Week
- April 19, 2024
- April 18, 2024
- April 17, 2024
- April 16, 2024
- April 15, 2024
- April 14, 2024
- April 13, 2024
Understanding Hoisting
Introduction
Before we talk about hoisting, it is important to understand how the JavaScript engine looks at the code, interprets, and runs it. Once we understand this, hoisting becomes straight forward to explain.
Execution Context
JavaScript engine to break up parts of the code to manage the complexity of interpreting and running it. If you 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'.
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:
var fruit = apple;function getFruit() { return fruit;}
Creation Phase
The below diagram depicts how the Global Execution Context looks during the creation phase.
In the GlobalCreation
phase, the JavaScript engine will:
- Create a global object.
- Create an object called this.
- Set up memory space for variables and functions.
- 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.
In the Global Execution
Phase, the JavaScript engine will:
- Starts running the code line by line.
- 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.
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 apple//The color is red
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 thats 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.
//The Variable x is initializedx = 5;//Output the value of x multiplied by 2 on the consoleconsole.log(x * 2);//The variable x is declared over herevar x;//Output -> 10
In the above code example, you would notice that the variable 'x' is initialised 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.
//The variable x is declared over herevar x;//The Variable x is initializedx = 5;//Output the value of x multiplied by 2 on the consoleconsole.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.
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 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:
Original Link: https://dev.to/skaytech/understanding-hoisting-b48
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To