5

can i ask you guys a question? Below is my code:

var num = 1;
var isNumberEqualOne = function(){
    if(num == 1){
      return true;
    }
    return false;
}();
alert(isNumberEqualOne);

In this code, the statement in the function return true, after return true, the code in the function is still executing right? So at the end, the code meet return false, so why the function still return true.Sorry for my bad english.Thanks

3 Answers 3

6

return will halt the function and return immediately. The remaining body of code in the function will not be executed.

In your example, num is assigned 1, so the condition inside your function is true. This means that your function will return there and then with true.

You could also rewrite that function so its body is return (num == 1).

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

Comments

6

As alex said, the return function transfers control out of the function call immediately; no other statements in the function (other than finally blocks) are executed.

So:

function foo(a) {
    if (a == 1) {
        alert("a is 1");
        return;
        alert("This never happens, it's 'dead code'");
    }
    alert("a is not 1");
}
foo(1); // alerts "a is 1" and nothing else
foo(2); // alerts "a is not 1"

Regarding what I said above that "no other statements in the function (other than finally blocks) are executed", more about finally blocks:

function foo(a) {
    try {
        if (a == 3) {
            throw "a is e";
        }
        if (a == 1) {
            alert("a is 1");
            return;
            alert("This never happens, it's 'dead code'");
        }
        alert("a is not 1");
    }
    catch (e) {
        alert("exception: " + e);
    }
    finally {
        alert("finally!");
    }
}
foo(1); // alerts "a is 1", then "finally!"
foo(2); // alerts "a is not 1", then "finally!"
foo(3); // alerts "exception: a is 3", then "finally!"

Note that no matter how the execution leaves the try/catch block, either naturally by falling out the bottom, or early because of return, or early because of an exception, the code in the finally block always runs.


Off-topic: Separately, note that you need parentheses around that function expression if you're going to call it immediately like that:

    var isNumberEqualOne = (function(){
//                         ^--- here
        if(num == 1){
           return true;
        }
        return false;
    })();
//   ^--- and here

or you can put the () that call it within the parens like this:

    var isNumberEqualOne = (function(){
//                         ^--- here
        if(num == 1){
           return true;
        }
        return false;
    }());
//     ^--- and here

Either works.

1 Comment

BTW, I was reading a JavaScript blog post yesterday titled Closures are not complicated. Imagine my surprise when I realised you were the author :P
0

When a function executes a return statement, it will not continue executing statements that appear after it. So if num == 1 evaluates to true, then the function will return true.

Note also that your alert statement is not calling the isNumberEqualOne function. You should do alert(isNumberEqualOne()) if you want the function to be called.

1 Comment

"Note also that your alert statement is not calling the isNumberEqualOne function." There is no isNumberEqualOne function. It's a boolean. He calls the anonymous function immediately upon defining it and stores the result of it in isNumberEqualOne.

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.