There are two types of Hoisting is there:
- Variable Hoisting
- Function Hoisting
Let’s first understand Variable hoisting with examples:
a=1; console.log("Variable a is hoisted at the beginning so this will print the value of a :->"+a); var a; // Output would be: Variable a is hoisted at the beginning so this will print the value of a :->1
But be cautious, If you initiate the value of variable at the bottom of page and try to use at beginning or middle of code that it will give undefined as output.
console.log("Hoisting won't work here. Hence, a is :->"+a); var a = 1; // Output would be: Hoisting won't work here. Hence, a is :-> undefined
Same way, Functions are being hoisted too. That we will learn in the next article.
Please let us know your feedback in comments. We will be more than happy to help to solve your doubts.