Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaration after return statement

Tags:

javascript

function f() { 
    return f1(); 

    function f1() { 
        return 5; 
    } 
}

f(); // returns 5

Why this works? What are benefits of declaration local functions after return? Is this good practice?

like image 521
Sergey Metlov Avatar asked Aug 30 '11 13:08

Sergey Metlov


1 Answers

It works because function declarations are all evaluated on a first pass by the interpreter, so you could place them all at the end of the function if you want, and they will work as though they were at the top.

No benefits. Just a preference. I prefer to have the return statement at the end of a function. Seems clearer to me.

like image 59
user113716 Avatar answered Sep 21 '22 11:09

user113716