Hoisting in JavaScript is the engine’s behavior of moving declarations to the top of their scope (global or local) before execution. Because of hoisting, you can reference functions or variables in your code before the lines where they are defined.
1.Function Declaration
Function declarations are hoisted in their entirety—both the declaration and the body. This means you can call a function before it appears in the source code.
hello(); //Output: hello!
function hello(){
console.log("hello!")
}
2.var Declaration
When you use var, JavaScript hoists the variable declaration, but not its assignment. Until the execution line reaches the assignment, the variable holds undefined.
console.log(num); //Output: undefined
var num = 10;
console.log(num); //Output: 10