Here is a tricky interview question that I am struggling to answer, much less give a clear explanation of why this code outputs undefined.
var x = 21;
var girl = function() {
console.log(x);
var x = 20;
};
girl();
So, the x = 20 is below the console.log - Javascript hoists the variable, but why doesn't it output it as 20? Ok, let's imagine, it ignores the variable that was declared below the console.log - why doesn't it look in the global scope? Who can make it clear for me? I'd appreciate it.