I'm trying to understand the example below:
(function (){
var func = function(){
console.log('foo')
}
function func(){
console.log('bar')
}
func();
})();
Why does this prints foo and not bar? Does it has something to do with variable hoisting?
I'm also confused by the code below:
(function (){
function func(){
console.log('bar')
}
var func = function(){
console.log('foo')
}
func();
})();
My logic says, that JavaScript will execute the first function it sees, but the output shows something else. Why this still produces the same output as the first example? Am I missing something?