Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is it proper to add a return at the end of a JavaScript function?

I have seen some developers place a return at the end of their JavaScript functions like this:

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

Is having a return even necessary? I know that return false cancels out of a loop early and I know that return x, returns a value, but having just return??? What does that mean?

Sorry - I forgot to put an end } at the very end of the code. the return is in the if conditional.


New Update - just discovered that the intent of the loop was to cancel out the nth .item that entered the loop. so return false is my replacement for a simple return; (which means undefined anyway). Thanks everyone for the help!

like image 734
klewis Avatar asked Jan 15 '23 10:01

klewis


1 Answers

Having return with not proceeding it at the end of the function doesn't really do anything. Returning "this" however allows for method chaining.

like image 121
Jordan Denison Avatar answered Jan 18 '23 23:01

Jordan Denison