-2

How about nested if?

$scope.addToCart = function () {
    if (flagA) {
        if (flagB) {
            if (flagC) {
                alert('nononono!');
                return;
            }
        }
    }
    someAnotherFunction();
};

I have a function:

$scope.addToCart = function () {
    var foo = 5;
    if (someFlag == 'Y') {
        alert('warning!');
        return;
    }
    someAnotherFunction();
};

I invoke this function somewhere

ng-click = "addToCart()"

My intention is to exit this function if

someFlag == 'Y'

then not to execute

someAnotherFunction();

But it still execute it.

WebStorm tell me this return is unnecessary so it could be safely removed.

ding van
  • 315
  • 2
  • 4
  • 14
  • 1
    `if (someFlag == 'Y') {` condition must be failing. Are you getting alert? – Tushar Aug 25 '16 at 15:07
  • Yes the alert worked. – ding van Aug 25 '16 at 15:09
  • 1
    Unexpected behaviour is usually what happens when code relies on global variables. – Mladen Ilić Aug 25 '16 at 15:10
  • @Mladen Ilić the `someFlag` is get from an API – ding van Aug 25 '16 at 15:12
  • 1
    @untmat [this](http://stackoverflow.com/questions/10459207/how-can-i-exit-from-a-javascript-function?noredirect=1&lq=1) answer says break could exit a loop not a function. – ding van Aug 25 '16 at 15:14
  • 3
    The current answers recommend to use an `else` block. While that will likely solve your problem, it's impossible that anything after the `return` statement is executed in the first place. You should figure out what the actual issue with your code is, not work around it. – Felix Kling Aug 25 '16 at 15:19
  • Could you post more code? Specifically where `someFlag` is set? – Seth Aug 25 '16 at 15:19
  • 1
    Your posted code works just fine: https://repl.it/Crm9 – mhodges Aug 25 '16 at 15:21
  • 2
    It is more likely that the `$scope.addToCart()` function is being called again (after `someFlag` has changed), or that `someAnotherFunction()` is being called from somewhere else, than it is that the `return` statement is not being executed immediately after the `alert()`. – Makyen Aug 25 '16 at 15:23
  • In response to nested ifs: if you just want to check conditions and do nothing else in the if blocks, `if (flagA && flagB && flagC) {...}`. The workaround proposed in the answers has the limitations, look here: http://stackoverflow.com/questions/1820839/using-return-instead-of-else-in-javascript – AgataB Aug 25 '16 at 15:37

2 Answers2

2

If you are sure that the if condition is satisfied, but return isn't working, then you can also try putting someAnotherFunction(); into else:

if (someFlag == 'Y') {
    alert('warning!');
    return;
}
else {
    someAnotherFunction();
}

That way someAnotherFunction() can be executed only if the if block isn't.

Addendum

The original code should work and this is just a workaround, as pointed out in the comment. However both methods achieve the same goal in theory.

For more details, check this question - for details on the differences.

Community
  • 1
  • 1
AgataB
  • 647
  • 2
  • 10
  • 18
0

You could put the call to the other function in the else-block:

$scope.addToCart = function () {
    var foo = 5;
    if (someFlag == 'Y') {
        alert('warning!');
        return;
    } else {
      someAnotherFunction();
    }

};

That should take you closer to understanding what is going on

Tobias Gassmann
  • 11,399
  • 15
  • 58
  • 92