In the code below:
(function (){
function test(){};//"function"
var test;//"undefined"
var printTest = typeof test;
document.write(printTest);
})();
printTest will display "function" instead of "undefined", which makes sense since from my understanding, any variable declarations are always "hoisted" to the top of the execution context (which in this case is function execution context) This makes the function declaration "test()" to be the one that appears later in the current execution context. Now consider this code where I actually assign a value to var declaration "var test =1".
(function (){
function test(){};
var test=1;//assign value to a variable here
var printTest = typeof test;
document.write(printTest);
})();
Then printTest displays "number" now, which means that the execution context now maintains a different order. Can somebody explain what has actually happened here?
;
at the end of thefunction test
line is evaluated as an empty statement which is useless at that position. Therefore, delete that semi-colons. – Nazler