JavaScript - Function Hoisting
JavaScript - Function Hoisting
Function Hoisting
Thefunction hoistingin JavaScript is a default behavior in whichfunction declarationsare moved at the top of their local scope before execution of the code. So, you can call the function in its scope before it is declared. Itβs important to notice that only declaration is hoisted not the initialization. So the variables or functions should have been initialized before they are used.
Similar to the function hoisting, the variable hoisting is also a default behavior in which the variable declarations are moved at the top of their local scope. We can use the function before it has been declared.
Letβs consider the following JavaScript code.
add(5,10); // 15
function add(x, y){
return x + y;
}In the above JavaScript code, the function add is called before itβs declaration. This is possible because JavaScript interpreter hoists the function declaration to the top of the scope. So the above code is equivalent to
function add(x, y){
return x + y;
}
add(5,10); // 15The function hoisting only works withfunction declarationnot withfunction expression. So, if the function is defined using thefunction expression,., it is not hoisted at the top.
add(5,10); // ReferenceError: Cannot access 'add' before initialization
const add = function (x, y){
return x + y;
}Letβs write some complete JavaScript examples of function hoisting.
Example: Function Hoisting
In the example below, we have defined the printMessage() function, printing the position from where it is called.
Also, we call the printMessage() function before and after the function definition. It prints the output without any error as the function is hoisted at the top of its scope.
Output
The function is called from the Top
The function is called from the BottomExample
In the example below, we have defined the function inside the βifβ block. So, the function is hoisted at the top of the βifβ block, and you can execute the function only inside the βifβ block before its initialization.
You canβt access the function outside the βifβ block.
Output
The function is called from the Top
The function is called from the BottomThe hoisting is very important behavior in JavaScript. But it is always advisable to declare the function or variable at the beginning of the code. Since JavaScript always interprets the code in sequence of declaration, then initialization and then usage.
JavaScript Variable Hoisting
The hoisting behaviors of JavaScript move the variables declaration to the top of the variableβs scope by default. In JavaScript, variables declared with the βvarβ keyword are hoisted at the top of its scope, but variables declared with the βletβ and βconstβ keyword is not hoisted at the top.
For example,
var x = y;
var y;The above code is similar to the below code, as the variables declaration is hoisted at the top.
var y;
var x = y;Letβs understand the variable hoisting by following examples.
Example: Variable Hoisting
In the example below, we have initialized the variable y, printed its value in the output, and declared it at the end. The below code prints a value without any error, as variable y is hoisted at the top in the global scope.
Output
The value of the y is : 10Example: Variable Hoisting with Function
In the example below, we have defined the printNum() function. In the printNum() function, we have initialized the variable y, printed its value, and declared it afterward.
The variable y is hoisted at the top of the function so you can access it before its declaration, but you canβt access it outside the function.
Output
The value of the variable y is : 20However, variable initialization is not hoisted at the top of the block.
Example: variable initialization is not hoisted
The example below demonstrates that the variable is hoisted at the top, but the variable initialization is not. Here, we print the variable x without initialization of it. So, it prints the βundefinedβ in the output.
Output
x is -> undefinedExample: Hoisting with let and const keywords
The below example demonstrates that the variable declared with the βletβ and βconstβ keywords are not hoisted at the top. So, you always need to define it before using it.
We used the βtrycatchβ block to handle the error. In the output, users can observe the error the below code gives when we access the variable declared with the βletβ keyword.
Output
Cannot access 'x' before initialization