What you think, will be the output of the following code ?
hello();
console.log(a);
function hello(){
console.log("Hello World!");
}
var a = 5;
console.log(a);
A small hint, the output of the above code snippet depends on : -
Hoisting - An interesting javascript concepts where variable and function declarations are allocated memory during context creation phase (phase 1) before execution
Javascript engine will start executing code line by line in the execution phase (phase 2)
Cheers!
Top comments (2)
I don't think hoisting applies to vars, so the first console.log(a) should cause an error to be thrown as "a" doesn't exist yet.
Hoisting is applicable to variables as well.
Both variables and functions are put into memory during context creation ( or compile ) phase.
Variables declared with var keyword will get initialized with value undefined during context creation phase.
So you would not get error for first console.log(a)