Javascript Hoisting For Multiple Declarations Of The Same Variable
I was trying to understand JavaScript hoisting and from what I have understood, memory space is set aside for all the variable declarations before the execution of the code. I woul
Solution 1:
Your code will be read by the interpreter like below,
functiona() {
console.log('hello');
}
var a;
console.log(a);
a = 2;
so while executing the above code, a
will be referring the function initially and after that var a;
line will be executed, since a
is undefined there, an assigned value will not be set with undefined
by means of a variable declaration. Hence that line got ignored and printing the primitive
value of the function reference.
A simple example for your better understanding would be,
functionx(){ };
var x;
console.log(x); //function x(){ }
Post a Comment for "Javascript Hoisting For Multiple Declarations Of The Same Variable"