-3

I would like to check for the else condition only. Is there a way that I can simplify the code below?

      if (_endCurrentGoal && _startCurrentGoal && _startCurrentGoal === _startEffectiveDate && _endCurrentGoal === _endEffectiveDate) { //no statement );
       } 
       else {
          console.log("check for this code only");
       }

Is below code correct? is there a way to simplify it?

       if(!(_endCurrentGoal && _startCurrentGoal && _startCurrentGoal === _startEffectiveDate && _endCurrentGoal === _endEffectiveDate) {
          console.log("check for this code only");
       }
Autonomous
  • 8,935
  • 1
  • 38
  • 77
noob
  • 45
  • 3
  • 11
  • yeah, that's correct but I would prefer first approach – Akash KC Feb 13 '17 at 23:33
  • but I doesn't need to check for the first if statement. just need to execute the else statement – noob Feb 13 '17 at 23:35
  • so sad, people just downvote my question. it is not that I'm not trying to solve it – noob Feb 13 '17 at 23:41
  • For me, first approach would be more clear. And as boolean operators are short circuit operator, it'll exit "if statement" if any expression from left to right would be false. – Akash KC Feb 13 '17 at 23:41
  • 1
    Please see: [Why is “Is this correct?” an off topic question, and what should I ask instead?](https://meta.stackoverflow.com/questions/359466/why-is-is-this-correct-an-off-topic-question-and-what-should-i-ask-instead) – EJoshuaS - Stand with Ukraine Jun 22 '18 at 16:33

1 Answers1

0

You can use De Morgan's Law to distribute the ! through the &&s.

That will look like this:

   if(!_endCurrentGoal || !_startCurrentGoal || _startCurrentGoal !== _startEffectiveDate || _endCurrentGoal !== _endEffectiveDate) {
      console.log("check for this code only");
   }