Why
var y = 1;
if (function f(){}) {
y += typeof f;
}
console.log(y); // 1undefined
Before running code I suppose it will be 1function
I suppose that behind function hoisting f should be visible for all code. Can you provide link for such behavior description?
P.S> at blog post where I find this example there is explanation
The output would be 1undefined. The if condition statement evaluates using eval, so eval(function f(){}) returns function f(){} (which is true). Therefore, inside the if statement, executing typeof f returns undefined because the if statement code executes at run time, and the statement inside the if condition is evaluated during run time.
But it doesn't make situation clearer
1functionvar f = function(){};before the if, and you'll get1function.eval