Alexander Mills Alexander Mills - 27 days ago 8
Javascript Question

Logical reduction to one line of code

Ok so my brain is a little fried after a long day of coding, but this one seems to be real. How can I reduce this to one if statement?

let combined = true;

if(earlyCallback){
combined = self.allChildBlocksCompleted;
}

if (self.parent && combined) { // i want to put everything here

}


is this correct?

if(self.parent || (earlyCallback && self.allChildBlocksCompleted)){

}


lol my brain is dead. I think it's right but I can't tell right now.

Answer Source

I prefer do it like below:

if (self.parent && (earlyCallback ? self.allChildBlocksCompleted : true )) { }