var text = \'outside\';
function logIt(){
console.log(text);
text =\'inside\';
}
logIt(); //prints outside. why?
I thought the
Because of hoisting the inner variable text
is moved to the beginning of the function. But only it's name portion:
var text = 'outside';
function logIt(){
var text;
console.log(text);
text ='inside';
}
logIt(); //prints undefined
Case 1 logs "outside", because text
is a variable in the surrounding scope of logIt
and hence accessible within logIt
. You reassign text
lexically after the console.log
call. So this reassingment isn't considered.