-1

I am executing some code based on condition of a particular variable and i am modifying the same variable inside the code.. How can i stop the if condition checking once the condition is met? When the else if condition is met and date2 is getting null the code is going back and checking if date2===null and executing both the condition.

           if(date){
               if(date2===null) {
                    //block of code
                    date2 = date;
                }
                else if(date2){
                    //block of code
                    date2 = null
                }
           }
  • 1
    May be use another variable as flag and once that flag is set don't proceed – Bharat Jul 22 '21 at 09:26
  • you can try using 'return' Ex- return date2 Use this in every code block – Ajinkya Jul 22 '21 at 09:26
  • Does this answer your question? [How do I "break" out of an if statement?](https://stackoverflow.com/questions/8523538/how-do-i-break-out-of-an-if-statement) – ssomename Jul 22 '21 at 09:27
  • *When the else if condition is met and date2 is getting null the code is going back and checking if date2===null and executing both the condition.* -- this code can't "go back", that would need something more, like a loop. – tevemadar Jul 22 '21 at 09:34

1 Answers1

0

var isConditionCheckRequired = true;
if(date){
   if(isConditionCheckRequired == true && date2===null) {
        //block of code
        isConditionCheckRequired = false;
        date2 = date;
    }
    else if(date2){
        //block of code
        date2 = null
    }
}
Bharat
  • 1,192
  • 7
  • 14