Javascript functions parsing order

javascript, parsing

Solution

Functions are 'hoisted' to the top of the scope they live in.

So your code actually reads:

(function f() {
    function f() { return 1; }
    function f() { return 2; }
    function f() { return 1.5; }
    return f();
})();

QED:

(function f() {
    function f() { return 1; }
    return f();
    function f() { return 1.5; }
    function f() { return 2; }
})(); //=> 2

Problem

Can someone please explain in great detail why in the following function, the return value is 1.5? Does javascript parse bottom to top or there is more to it? ``` (function f() { function f() { return 1; } return f(); function f() { return 2; } function f() { return 1.5; } })(); ```

Original source

Related problems