i have this piece of code:
(function f(){
function f(){ return 1; }
return f();
function f(){ return 2; }
})();
why does this code print '2'?
i have this piece of code:
(function f(){
function f(){ return 1; }
return f();
function f(){ return 2; }
})();
why does this code print '2'?
In javascript, function definition is hoisted to the top of its containing function.
Your function is interpreted by the browser like this:
(function f(){
//Functions defined first
function f(){ return 1; }
function f(){ return 2; } //<- Hoisted to top, now this is what f is
//Now the rest of the code runs
return f();
})();