This affects what happens when execution introduces a function. Leaving a lot of detail, all function declarations (the style that you used) are processed, and only after that does the step-by-step code execution take place. Therefore, your return does not affect which function is declared. And the chosen declaration is always the last in the source code (this is covered - in the marvelous turgid prose - in Section 10.5 of the specification).
The result will be fundamentally different if you use function expressions that are evaluated as part of a step-by-step code:
(function() { var f; f = function(){ alert(1); }; return f(); f = function(){ alert(2); }; })();
This code is technically incorrect (because you have code following return that will always execute), but it illustrates the difference: you see alert(1) , not alert(2) , because these expressions are not evaluated before those until they are achieved.
You can learn more about what happens when execution enters a function (declarations are not the only ones executed before the first step-by-step code) in sections 10.4.3 and 10.5 of the specification.
And with your new knowledge, quiz: what is happening here? (Note: Never do this.)
function foo() { if (true) { function bar() { alert(1); } } else { function bar() { alert(2); } } bar(); } foo();
Answer: He is changing, do not do this. Some engines will use the first bar , other engines will use the second, and others will cause it with a syntax error. This is due to the fact that there is actually a mistake, and therefore the engines can do what they consider the best. If you look closely at the grammar of the language, you will see that it is not valid to place function declarations inside branches within their immediate contents. They should be at the top level of this sphere. With a new understanding of declarations, the reason should be obvious: they are not related to the flow of execution within the scope, and therefore, naturally, you cannot select them based on this flow of execution.
So what is going on in the real world? Unfortunately, this is usually not a mistake if you are in "free" mode (not strictly). Some engines (e.g. Chrome V8, at the time of this writing) will ignore the flow control statements and simply select the last declared function (and therefore you will get a counter-intuitive result that the second bar function is used), other engines (Firefox SpiderMonkey, IE11 JScript ) effectively rewrite your code on the fly, turning these declarations into expressions instead, and so you get the first bar . For example, it depends on the engine. The good news is that if you try this in strict mode, all three of them (V8, SpiderMonkey and IE11 JScript) fail, and not picking one (V8 and SpiderMonkey with good clear error messages in the console; JScript with an amazing " bar - undefined ", but ...).
If you want to do something similar above but are valid and consistent across machines, use expressions:
function foo() { var bar; if (true) { bar = function() { alert(1); }; } else { bar = function() { alert(2); }; } bar(); } foo();
There's an interesting study of this subject on the kangax Named Function Expressions of the Demystified page .