admin管理员组文章数量:1301509
Say you have 2 identical functions that do not return a value
function a() {
// do some interesting things
}
function b() {
// do the same interesting things
return;
}
Function b
is obviously more verbose but is there any functional difference between these?
Say you have 2 identical functions that do not return a value
function a() {
// do some interesting things
}
function b() {
// do the same interesting things
return;
}
Function b
is obviously more verbose but is there any functional difference between these?
- 2 A good answer to this should include a link to the relevant part of the ECMA spec. – georg Commented Feb 24, 2012 at 18:04
- 2 return; basically means terminate function here. So if you have it as the last bit of your function it does not do anything useful. – gintas Commented Feb 24, 2012 at 18:09
- 1 12.9 in the spec, for those interested. – davin Commented Feb 24, 2012 at 18:50
3 Answers
Reset to default 11There's no real difference; both will return undefined
.
Functions with no return statement will return undefined
, as will functions with an empty return
statement.
To confirm this for yourself, you can run this code -- FIDDLE:
function a() {
}
function b() {
return;
}
var aResult = a();
var bResult = b();
alert(aResult === bResult); //alerts true
Adam is correct; both functions return undefined, and either way is absolutely fine if you don't care about the return value (or desire the value to be undefined). However, it's often better in more plex programs to explicitly return from a function, especially since Javascript programs often have plex callback mechanisms. For example, in this piece of code (just a little more plex than yours) I believe the return statement really helps clarify the code:
function someAsyncFunction(someVar, callback) {
// do something, and then...
callback(someVar);
// will return undefined
return;
}
function c(){
var someState = null;
if (some condition) {
return someAsyncFunction(some variable, function () {
return "from the callback but not the function";
});
// we've passed the thread of execution to someAsyncFunction
// and explicitly returned from function c. If this line
// contained code, it would not be executed.
} else if (some other condition) {
someState = "some other condition satisfied";
} else {
someState = "no condition satisfied";
}
// Note that if you didn't return explicitly after calling
// someAsyncFunction you would end up calling doSomethingWith(null)
// here. There are obviously ways you could avoid this problem by
// structuring your code differently, but explicit returns really help
// avoid silly mistakes like this which can creep into plex programs.
doSomethingWith(someState);
return;
}
// Note that we don't care about the return value.
c();
Generally you are returning a value. So for instance,
function b() {
return 'hello';
}
a = b();
console.log(a);
Will output "hello" to your console.
本文标签: javascriptIs there a difference between a function with and without a return statementStack Overflow
版权声明:本文标题:javascript - Is there a difference between a function with and without a return statement? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741677620a2391977.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论