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
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
Share Improve this question edited May 31, 2011 at 6:35 alex 490k204 gold badges889 silver badges991 bronze badges asked May 31, 2011 at 6:14 dramaseadramasea 3,49017 gold badges53 silver badges77 bronze badges3 Answers
Reset to default 6return
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)
.
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.
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.