11

I want to exit from the below if block in Javascript. if I return, then it does not check for the next if condition. How do I do that?

if ($('#id1').length > 0) {

    if(yester_energy == "NaN" || yester_energy == 0){
      //break from #id1
    }

    else{
      //something
    }
    $("#abc").html(somthing)
}

if ($('#id2').length > 0) {

        if(yester_energy == "NaN" || yester_energy == 0){
          //break from #id2
        }

        else{
          //something
        }
}
2
  • You don't have to do anything special. The else blocks won't be executed if the if conditions are fulfilled. Commented Nov 22, 2013 at 7:25
  • Sorry I forgot to add one line. Kindly see the updated question....Thanks. Apology Commented Nov 22, 2013 at 7:42

7 Answers 7

23

Super late to the party, but for folks from search, you can use something called labeling. It's not good practice, but in rare cases that can be applied. Basically you can assign a name to the if statement that you want to break from. And anywhere in statement call break from specified name.

Code example:

my_if: if (condition) { 
    // do stuff
    break my_if;
    // not do stuff
}

in your particular case:

id1: if ($('#id1').length > 0) {
    if(yester_energy == "NaN" || yester_energy == 0){
        break id1;
    }else{
      //something
    }
    $("#abc").html(somthing)
}

More about labeling can be found here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/label#Syntax

Sign up to request clarification or add additional context in comments.

3 Comments

fantastic answer
Note that goto statements are generally not a good practice.
@ChrisP There is no goto statements. It's just the defining the block you want to leave.
8

Even later to the party... Typically in such situations, I use a combination of if and do...while as follows...

if ( condition ) do {

  // processing

  if ( exit_condition ) break;

  // more processing...

} while ( false );

Thus, when the break is encountered, it applies to the do...while loop, and processing continues after the while, effectively breaking out of the outer if statement...

Comments

2

All your code after if(isNaN(yester_energy) || yester_energy == 0) is in else block, so it'll not be executed if your data matches this if. And you just don't need anything else.

Also, if you want to check if variable got NaN value, then use isNaN() function. You can't just compare it.

3 Comments

Sorry I forgot to add one line. Kindly see the updated question....Thanks. Apology
@prafulbagai That changes nothing, just move this line to else block.
I did that before looking at your answer. THanks. Silly me
0

Use switch statment,

   switch(n)
    {
    case 1:
      execute code block 1
      break;
    case 2:
      execute code block 2
      break;
    default:
      code to be executed if n is different from case 1 and 2
    }

Comments

0

you can use function and pass command

function iff(condition){
 if(condition ==0) return;
 console.log("other commands")
}


iff(0);
iff(1)


 
 

Comments

0

Adding another answer to the pool, in many cases using a try catch makes perfect sense:

if(something is allowed) {
  try {
    if(missing requirements) throw 'missing requirements'
    //do your stuff;
  }
  catch {
    //you may do logging and issue warning here
  }
}

Comments

-1

Don't Use Labeling

You can do labeling as the other answer suggests, but it smells odds and it will definitely come up in code review. It will confuse readers of your code and increase the maintenance burden of your codebase.

Answer

Best is to refactor (extract out) the code inside the if-statement into a separate function that you can then return from. In addition to allowing you to exit from the code block early, it also makes your code more modular and readable.

Example (based on OP)

if ($('#id1').length > 0) {
    ifInner(yester_energy)
    
    $("#abc").html(somthing) // [sic]
}

if ($('#id2').length > 0) {
    ifInner(yester_energy)
}

// Rename to something more descriptive.
function ifInner(yester_energy) {
    if (yester_energy == "NaN" || yester_energy == 0){
        // Break out.
        return;
    }
    else {
        // something
    }
}

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.