Recently I was confused about javascript hoisting behavior and now I got stuck with that.
So, there are two examples.
var alpha = 'alpha';
var beta = 'beta';
f(); //beta
var f = function f1() {
console.log(beta);
};
function f() {
console.log(alpha);
}
f(); // alpha
The first one is working as expected, because the function declaration overwrite our variable f (with value "undefined") when Javascript is set up the Lexical Environment.
But the second gives me a push, that I does not understand something.
var alpha = 'alpha';
var beta = 'beta';
f(); // - alpha
function f() {
console.log(alpha);
}
var f = function f1() {
console.log(beta);
};
f(); // beta
Why the variable f did'nt hoisted to the top of out code, and overwrite our hoisted function before? Why in "first f" call I recieve "alpha". I think, that it must be an error like: "f is not a function", because in first f call I excepect, that the f would be "undefined".