Is 'return' necessary in the last line of JS function?

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.


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);

Here's a fiddle.


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.


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.

Tags:

Javascript