Conceptually variable hoisting is often presented as the interpreter “splitting variable declaration and initialization, and moving (just) the declarations to the top of the code”. In the below example you are getting the value if
undefined in the first line itself, that means it has been intialized in the Global scope before the line of decalaration in the code.
console.log(num); // Returns 'undefined' from hoisted var declaration (not 6) var num; // Declaration num = 6; // Initialization console.log(num); // Returns 6 after the line with initialization is executed.
The same thing happens if you declare and initialize the variable in the same line.
console.log(num); // Returns 'undefined' from hoisted var declaration (not 6) var num = 6; // Initialization and declaration. console.log(num); // Returns 6 after the line with initialization is executed.