[thelist] "return" in JS question

Means, Eric D eric.d.means at boeing.com
Tue Jun 4 15:57:17 CDT 2002


>  -----Original Message-----
> From: 	Tom Dell'Aringa [mailto:pixelmech at yahoo.com]
> Sent:	Tuesday, June 04, 2002 3:43 PM
> To:	thelist at lists.evolt.org
> Subject:	RE: [thelist] "return" in JS question
>
> So the return statement in this function is useless then, wouldn't it be?
It's not even sending a
> value so it would be undefined..
>
> function mouseUp(e) {
>  var x = event.x + document.body.scrollLeft;
>  var y = event.y + document.body.scrollTop;
>  isActive = false;
>  return;
> }

Basically.  The closing brace implies a return statement, so the return; is
redundant.  It's often put in for one of a couple of reasons:
1. Clarity.  It can be easier to spot a return; than it can be to spot which
of a series of closing braces belongs to the function.
2. Exiting the function from nested braces:
function whatever () {
  if (something) {
    ...
    if (something_else) {

    } else {
      // something is wrong, exit the function
      return;
    }
  }
}
3. Having a final return statement can make sure that you *know* what the
function will return if it manages to get all the way to the final brace; in
this case it almost always has an argument, however (return true, return 0,
etc).



More information about the thelist mailing list