I have three variables, variation
, attribute
and active
for which I need to create an if
statement for the following truth table:
variation | attribute | active | Result |
---|---|---|---|
[object Object] | 'color' | true | TRUE |
undefined | undefined | undefined | TRUE |
[object Object] | 'color' | false | FALSE |
But I can't think of a smart and compact statement for the case, unless I write all desired true
scenarios in an or
separated long statement...
Can someone please help me with this?
Here is a quick fiddle for you.
TIA.
You can negate all the values to booleans and check if they are all equal:
!variation == !attribute && !attribute == !active
Values in JavaScript can be either truthy or falsy.
So when you put the logical NOT operator (!
) before a value, you effectively negate it, meaning all truthy values become the boolean false
and all falsy values are cast to the boolean true
.
Your truth table seems to return true when all of the variables are truthy or when all of the variables are falsy, and returns false when they are mixed.
So it essentially checks that all the values, when casted to booleans, are equal. So the condition above casts the values to opposite booleans and checks that they are all the same.