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!
Your example is specific for how
jQuery.eachhandles the return values from a loop function. From the docs:So there you have it, returning anything else than
falsemakes no difference, it will just move on to the next iteration.false, however, breaks the loop.Note that
return;is exactly the same asreturn undefined;.