Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

javascript

what's the best practice of finishing the JS function if it doesn't return anything?

function (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.

like image 608
etual Avatar asked Oct 19 '16 19:10

etual


People also ask

Do you need return at the end of a function?

Without a return, you'd just be performing a function with a dead end. To add some further context specific to the answer, if the question boils down to "Do I need to add a return for the sake of it, at the end of a function, then the answer is no.

Is return mandatory in JS function?

A JavaScript function can have an optional return statement i.e. it's optional to return a value. This statement should be the last statement in a function.


4 Answers

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.

like image 55
rabbitco Avatar answered Sep 28 '22 05:09

rabbitco


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.

like image 38
Brynden Bielefeld Avatar answered Sep 28 '22 04:09

Brynden Bielefeld


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.

like image 31
I wrestled a bear once. Avatar answered Sep 28 '22 06:09

I wrestled a bear once.


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.

like image 40
Jose Quijada Avatar answered Sep 28 '22 05:09

Jose Quijada