Reputation: 561
What's the best practice for finishing a JavaScript function if it doesn't return anything?
function foo(a) {
if (a) {
setTimeout()
}
return;
}
In this case return
is unnecessary but I leave it for readability purposes. I also tried to google if this is a way to micro-optimize JS but wasn't able to find anything.
Upvotes: 30
Views: 15298
Reputation: 692
Arrow functions (search for Remove the body brackets and word "return" -- the return is implied
) which body is a single line and without curly braces do not require a return
statement, even for non void
functions. Example.
var returnTest = () => 1 + 1;
returnTest(); // Outputs 2
var returnTest2 = () => { 1 + 1};
returnTest2(); // Outputs undefined
AFAIK and aside from void
functions, this is the only instance in which return
may be ommitted.
Upvotes: 3
Reputation: 2850
what's the best practice of finishing the JS function if it doesn't return anything?
A function returns undefined
by default. The use of return
is therefore only necessary if you want to override the default behaviour.
Why most leave it out when they can
The Javascript community seem to dislike unnecessary verbose code. It might even be said to be a sport to make the code as short and compact as possible.
Why this may not be the best practice for all
The habit of always using return may serve as a good reminder that a function always returns something and thereby remind you to reflect on whether the default behaviour should be overridden.
However an experienced programmer will have such considerations deeply internalized and therefore rarely have need for such reminders.
Conclusion
As one gains experience the sport for less verbose code intensifies and in that context writing code just confirming default behaviour is an absolute no-go.
So: in the long run I would say most people end up leaving it out and that it is justified to do so because of the high degree of internalization.
Upvotes: 23
Reputation: 11661
As the other people have stated, its not necessary. But some people find it good practice. especially when you love being declarative and you know return statement is nothing more than actually a GOTO
statement. (think of your programming being completly linear and you can only move between code with labels and goto statements). This way you always declare where you are going to: you are going back.
For example you can have the following function
function login(user, pass){
if(user === null)
return;
doTheRest(user, pass);
}
This way in a void method you can return 'early' to make sure you dont continue when a condition is not made. This is whats also called defensive programming.
note: most of the time your better of throwing an error throw new Error(...)
instead. This way you realise earlier you forgot to match a pre-condition.
Upvotes: 2
Reputation: 23379
Just for fun and FYI I wrote a little benchmark script to try it out.
To my surprise, the one with the return statement returned faster than the one without by about 100ms for about a dozen runs on Firefox (Gecko). This result was pretty consistent on both Mac and Ubuntu.
I also ran it on Chrome (WebKit) and the results were much more ambiguous, there wasn't much consistency at all.
These were my results and are in no way definitive. Feel free to try it for yourself. In my opinion your personal preference is more relevant than 100ms anyway. If that's how you roll, then add it.
function f1(){
console.log("doing stuff 1");
}
function f2(){
console.log("doing stuff 2");
return;
}
var start = (new Date()).getTime();
var i = 1000; while(i--) f1();
var time1 = (new Date()).getTime() - start;
var start = (new Date()).getTime();
var i = 1000; while(i--) f2();
var time2 = (new Date()).getTime() - start;
console.log("Without return: "+time1);
console.log("With return: "+time2);
Upvotes: 4
Reputation: 1503
There is no need to use return
statement but it's good practice to use void
operator which force the function keyword to be treated as an expression instead of a declaration.
But then your function should look like that:
void function (a) {if (a) {setTimeout()}}
If you declare a non-immediately-invoked function expression, your should:
return true
or
return !failure;
Upvotes: 2
Reputation: 13425
Adding an unnecessary return
statement won't make any real difference in terms of optimization. Write code for humans and not computers.
It's not need if you won't use function to get a result (i.e. sum(2, 3)
), such as:
function xxx(){
post().then(function(){
location.href('http://stackoverflow.com');
}, function(){
showError();
});
}
You see I have 3 functions and all have not return
statement, and it's just fine.
Entering in opinion lands, I would say that you could leave it for your readability and comfort, but you would have an approach different from most of programmers. And as less is better (KISS), and if it's not necessary at all, it's better to omit it.
PS: The result of a function without return
, will be undefined
, naturally.
function a(param){
var x = param;
}
function b(param){
var x = param;
return;
}
function c(param){
var x = param;
return x;
}
a(5); // undefined
b(5); // undefined
c(5); // 5
Upvotes: 2
Reputation: 37663
No, a return
statement is not necessary at the end of a void function (sorry for the C-terms there). If you want to exit a function early, however (say if a specific condition wasn't met), then you'll use a return
statement, even if you're not returning a value.
Including a return statement at the end of a void function for readability, should be fine. I honestly don't think doing so is going to add much to your footprint.
In my opinion, however, you shouldn't include a return statement at the end of a function that doesn't return anything.
Upvotes: 10