0

I have a function that looks like this:

function MyFunction() {

  //do some work

  if (SomeCondition === true) {
     //do some work
     FunctionToCall();
  }
  //do some more work
}

function FunctionToCall() {...}

If ever I call the FunctionToCall(), I don't want to continue with do some more work. Do I need to put a return true or return false statement after the FunctionToCall(); statement? It seems to work with both.

Thanks.

1
  • You just need to return; It doesn't have to be true or false. Commented May 31, 2012 at 13:01

4 Answers 4

4

As you said, you would need to put a return after the call to FunctionToCall. By the sound of it, it doesn't matter what you return (this will return undefined):

if (SomeCondition === true) {
    //do some work
    FunctionToCall();
    return;
}

If you did care about the result of FunctionToCall, you could return the value returned from that:

return FunctionToCall();

Alternatively, you could use an else, and not bother with a return statement (this will return undefined too):

if (SomeCondition === true) {
    //do some work
    FunctionToCall();
}
else {
    //do some more work
}
Sign up to request clarification or add additional context in comments.

Comments

3

You can just do a return;, since you are ignoring the return value of the function.

Comments

1

yes you will have to put a return statement after or just before a call to next function like this

return FunctionToCall();

or

FunctionToCall();
return;

Comments

0

If you are always using MyFunction() in a void-context, use return;

If FunctionToCall() might also be invoked in a different context, I prefer putting return true/false in the next line.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.