Exit from if block in Javascript

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
        }
}

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

Exit from if block in Javascript, The break statement, without a label reference, can only be used to jump out of a loop or a switch. With a label reference, the break statement can be used to jump � Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java and XML.


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.

JavaScript Break and Continue, What I am not sure about is what's the proper way to exit a js if loop, do I The break statement breaks the loop and continues executing the� Return will exit the function but will continue with the script. Break will stop execution of the rest of the script. Exit will exit the script and close the console. In regard to Exit it depends on what you want to achieve afterwards. Exit 0 will return the exit/errorcode of 0. Exit ‘any none zero’ value will produce the exit/errorcode of 1.


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...

Proper way to exit an if loop in javascript, The break statement terminates the current loop, switch, or label function testBreak(x) { var i = 0; while (i < 6) { if (i == 3) { break; } i += 1; }� "Add a descriptive comment if the default block (else) is supposed to be empty. Moreover, if that block is not supposed to be reached throw an InvalidOperationException to detect future changes that may fall through the existing cases. This ensures better code, because all paths the code can travel has been thought about."


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
    }

ifelse, I need away to exit and if statement if a condition is meet, but stay in the function and continue with the rest of it. Something like this (but works):� A switch statement can replace multiple if checks.. It gives a more descriptive way to compare a value with multiple variants. The syntax. The switch has one or more case blocks and an optional default.


break, This tutorial shows you how to use the JavaScript break statement to control the execution of code in a loop and how to exit the nested loop. The break statement exits a switch statement or a loop (for, for in, while, do while). When the break statement is used with a switch statement, it breaks out of the switch block. This will stop the execution of more execution of code and/or case testing inside the block.


JavaScript exit if statement, if statement (ES1); switch statement (ES3); while loop (ES1); do-while loop (ES3) Labels can be put in front of any statement, including blocks. break foo leaves� In this tutorial, we will learn about how to exit the function in JavaScript. Sometimes we need to exit the middle of functions in that cases there is a return keyword in JavaScript which helps us to stop the running function. The return keyword exits the function by returning a undefined. Example:


JavaScript break: Control The Execution of Code In a Loop, If the next case matches the expression, the code executes and exit out of the switch block. If no cases match, the default code block is executed.