0

JavaScript allows this:

function outside() {
    inside();
    return 44;

    function inside() {
        console.log("inside");
    }
}

When outside is called the output is:

inside
44

Why does this work and why doesn't it work for other statements like the one below:

function outside() {
    return 44;
    console.log("inside");
}

which just prints 44.

1
  • it is because it is calling inside() before return so it is executing,it doesn't matter where you place function. Commented Dec 21, 2013 at 8:06

5 Answers 5

2

What you see is the effect of hoisting. When a function is about to be executed, all the variable and function declarations are evaluated first, before the function is really executed. Thus your first function is equivalent to

function outside() {
    function inside() {
        console.log("inside");
    }
    inside();
    return 44;
}

Details can be found in the specification, 10.4.3 Entering Function Code and 10.5 Declaration Binding Instantiation.

Why does JavaScript allow function declarations after the return statement?

Why the language is defined like this can probably only be answered by Brendan Eich.

Sign up to request clarification or add additional context in comments.

Comments

2

Because the file is parsed first and function definitions are read at that time. You are calling the function before you return, so that works just fine. Most programming languages allow you to define functions after calling them, because they all work in two steps: parsing and execution.

6 Comments

I don't think anything is evaluated at parse time.
Nitpicky. ;-) Changed my wording.
Everything is read during parsing. Parsing has nothing to do with this behavior.
@zero Yes and no. It explains why this behavior is possible in the first place. If you expect a program to be read and executed line by line, the behavior doesn't make any sense. If you can make the leap to distinguishing between parsing of function declarations and runtime, the behavior is almost self-explanatory. The language could still enforce rules to require function declaration before execution, but that would actually be an artificial restriction.
Nobody expects that, I hope :D Still, just because the compiler knows that there is a function declaration, does not mean that the function exists. In fact, when you call outside inside does not exist yet. That's not an artificial restriction. Moreover the parser itself could be created so that it ignores or discards everything after the return statement. In that case the compiler wouldn't even know about the function declaration. So it's really not about parsing. Apart from the line by line thing :)
|
0

In the first case first the function inside is executed and then it comes to return..so it returns 44..

But in the second case the outside function first encounters return which means to exit from that function no matter what is return below... so it only prints 44

Comments

0
function outside() {
inside(); //calling inside();
return 44;

function inside() {
    console.log("inside");
}
} 

but here

function outside() {
return 44;
console.log("inside");
}

you are just returning not calling inside() at all.

Comments

0

Function declaration:

function myName() {

}

Function Expression:

var myName = function() {

};

These are very different, the function declaration (1) is defined when the Javascript is parsed, and not when it is executed. whereas the function expression (2) is defined when the Javascript is executed.

So technically it is not being defined after the return statement.

At least this is how I understand it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.