Because if/else statements have the scope of whatever they're inside of (whether global or inside of another function). Once you've exited the if/else logic, you will have defined foo one way or the other.
In this case, if true
is true (which it is) then foo would be set to that first function. If it weren't true, foo would still be set to the second function.
You're correct that variables inside of functions go out of scope, but the same is not true for if/else statements.
For example:
a = 5;
var add_ten = function(a){
b = a + 10;
return b;
}
c = add_ten(a)
console.log(b) // B will not be set
if(a > 1){
b = a + 10;
}
console.log(b) // B will be set
This is a good thing: You want to be able to use those variables later.
EDIT: I should mention that, as the comment below detailed, conditionally declaring functions is a bad practice, and should be avoided. Instead, you should declare two functions with two different names outside of your if/else statement, and conditionally call one of them. When one is triggered, the other will not execute even though it has been defined, so there's no risk of both running (mentioned in case you were worried about that).