JavaScript type hoisting with variable function Expression
Hoisting is JavaScript default behavior to moving declaration on the top. Variable can be used before its declaration. To understand this, first we need to understand of hoist meaning, hoist means “raise(something) by means of ropes & pulleys”. now we can relate hoisting with JavaScript. JavaScript interpreter moves all declaration on the top at runtime.It is best practice to declare all variable at the top of their respective scopes. , JavaScript is loosely syntax language so it will not throw any error at runtime if the variable used before its declaration.
But JavaScript does not allow variable used before its declaration in “use strict” mode.
Example 1
In this example, we can see in a normal scenario, when y has declared & then used. in this case, it will give output 10 as excepted.<p id="demo"></p>
<script>
var y; //Declare y
y = 10; //Assign 10 to y
document.getElementById("demo").innerHTML = y; //y used here after its declaration
</script>
Example 2
In this example, we can see JavaScript hoisting behavior use y before its declaration. & output would be 10.Here’s what is happening behind the scenes: declaration getting hoisted at the top
<p id="demo"></p>
<script>
y = 10; //Assign 10 to y
document.getElementById("demo").innerHTML = y; //y used here before its declaration
var y; //Declare y
</script>
JavaScript only hoists declarations. Initialization does not hoist.
Example 3
var a;
console.log(a); // undefined
a = 3;
We expected 3 in output but instead, undefined is logged. Why is this? It’s because JavaScript only hoists declarations. Initialization does not hoist (JavaScript interpreter looks ahead and “hoists” all variable declarations to the top, and the initialization remains in the same spot).
Function Hoisting
a certain type of function declaration follows the same behavior as the variable does. but some functions do weird behavior. we can explore below examplesExample 1
hoistFun(); //function hoist too
function hoistFun(){
console.log("function hoisting") // out put "function hoisting"
}
In the above example, the JavaScript interpreter hoisted hoistFun function on the top as other variable declaration.
Example 2
// TypeError: hoistFun is not a function
hoistFun();
var hoistFun= function (){
console.log("function hoisting")
}
In the above example function will not hoist if it is part of function expression, why is this, because you can not use function expression before you define them.
Now, let's take same hoistFun function expression define first, then called a hoistFun function. it will not throw any error.
Example 3
var hoistFun= function (){
console.log("function hoisting") // out put: function hoisting
}
hoistFun();
Example 4
In this example created a name variable as a global variable, & created the same variable as a local variable formyFunc function.
var name = "jhon";
function myFunc () {
// Outputs: My name is undefined
console.log("My name is " + name);
var name = "smith";
// Outputs: My name is smith
console.log("My name is " + name);
}
myFunc ();
In this case, we expected the value of name from the outer scope, but same name variable(local variable for function ) declared & initialized inside function after the logged value of name but due to initialization does not hoist so the name is undefined.
Nice.
ReplyDeleteThank you!!!
DeleteNice article Amritanjali!
ReplyDeleteNice one...easy to understand concept of hoisting
ReplyDelete