admin管理员组文章数量:1221252
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.
本文标签:
版权声明:本文标题:Question about if statement in function in javascript - how if the if statement return true but the function return false - Stac 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1739361018a2159818.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论