Because you don't compare the same thing. In your example - you compare function declaration function foo()...
with variable declaration and assignment in var foo = 'bar';
A more correct comparison would be of:
console.log(foo);
var foo = 'bar';
with
console.log(foo());
var foo = function() {
return 'bar';
}
The functional declaration is interpreted differently due to the way hoisting works. Hoisting moves all declarations to the top of the closest scope, while leaving assignments in their place.
Function declaration is special in that sense, since it's both declaration and expression/assignment in one statement and thus hoisted together.
As an example: you can look at expressions like:
console.log(foo);
var foo = 'bar';
as this:
var foo;
console.log(foo); //prints undefined
foo = 'bar';
and
console.log(foo());
var foo = function() {
return 'bar';
}
as this:
var foo;
console.log(foo());
foo = function() {
return 'bar';
}