When do I need to add a return at the end of a JavaScript function?

I have seen some developers put a return at the end of their JavaScript functions as follows:

$(".items").each(function(){ mthis = $(this); var xposition = some .x() position value; if(xposition < 0){ mthis.remove(); return; } }); 

Is a refund required? I know that return false cancels the loop earlier, and I know that return x returns a value, but just returned ??? What does it mean?

Sorry, I forgot to end} at the very end of the code. return is in the if condition.


New update - just discovered that the goal of the loop was to undo the nth element that went into the loop. so return false is my replacement for simple return; (which means undefined). Thank you all for your help!

+4
source share
6 answers

Your example shows how jQuery.each handles return values ​​from a loop function. From the docs:

We can break the $ .each () loop at a specific iteration, making the callback function return false. Returning non-false is the same as continuing in a for loop; he will immediately proceed to the next iteration.

So, you have this, returning something else than false does not matter, it will simply proceed to the next iteration. false , however, aborts the loop.

Note that return; Exactly matches return undefined; .

+2
source

Returning without continuing at the end of the function does not actually do anything. Returning "this" however allows a chain of methods.

+3
source

This is just a way to end a function, it is not necessary to select the correct return value. At the end of the function, it really doesn't matter.

+1
source

Like return false , it will sooner or later cancel the block (in this case, the function). The return value will be undefined .

At the end of the function, this is equivalent to not using the return statement at all, but your example is not like the end of the function to me.

EDIT: Just to make it clearer: this particular return statement is at the end of the surrounding if clause, not the function as a whole. In other words, the condition of the if clause indicates the situation when the function should end in the middle.

EDIT 2: Under “function”, I refer to an internal anonymous function. return; , equivalent to return undefined; , will actually cause jQuery.each() move on to the next element. (It also seems that I was mistaken in the fact that the function has more code, since when editing the OP in the code sample, it really looks like a meaningless return statement.)

+1
source

The problem is that your opening and closing brackets { } do not line up, so depending on what is correct, this will work differently. If the closing bracket for if is after the return, then it will act as a continue in the regular loop.

 $(".items").each(function(){ mthis = $(this); var xposition = some .x() position value; if (xposition < 0) { mthis.remove(); return; } } 

As it turned out, a continue at the end of the for loop is also implicit, so it really doesn't serve the purpose.

Of course, if you doubt the more theoretical, the answer is that in general it does not matter:

 function doSomething() { alert("Hello, world"); return; //purely a matter of preference } 

Here, the method will implicitly return even without a direct call to return due to the fact that it is at the end of the code block; in some cases, this can be useful as an indicator that you are at the end of the function, especially if there was a lot of nesting. If not at the bottom, it definitely matters, since after return; nothing will be done. Otherwise, the difference is purely stylistic. Save you a few characters to leave him, of course!

+1
source

Return behavior; anywhere in your function, just as when the code reaches the end of your function, the caller gets undefined.

In this code, it is used along with the condition. I should have assumed that you did not provide the complete code.

This method is often used to jump to the end of a function when a precondition is not fulfilled, since it can be more readable than five indents, each with a different condition.

+1
source

Source: https://habr.com/ru/post/1437589/


All Articles