To understand function hoisting, let us start by considering the code listed below:
- Declaration of variable
The declaration of a variable goes at the top of the execution context, and assignment happens where the variable is created. So the above code snippet is broken into two statements as shown in the image below:
The variable foo is hoisted at the top of the execution context of function abc, hence when you use it before its declaration, you get “undefined” as the output.
Keep in mind that a variable declared using the let statement does not get hoisted to the top of the execution context.