if (a is not 1 and not 2) vs if(a is not 1 and 2)

if (a is not 1 and not 2) vs if(a is not 1 and 2)
typescript
Ethan Jackson

Straightforward question, how can I differentiate these ifs?

if (a is not 1 and not 2) { ... }

and

if (a is not 1 and 2) { ... }

As I understand it, first one can be written as if (a != 1 && a != 2), but what exactly the second one means? Would it be if (a != 1 && a == 2) ?

Answer

Would it be if (a != 1 && a == 2) ?

Yes. You can see the syntax of patterns here, not has a higher precedence than and.

pattern : disjunctive_pattern ; disjunctive_pattern : disjunctive_pattern 'or' conjunctive_pattern | conjunctive_pattern ; conjunctive_pattern : conjunctive_pattern 'and' negated_pattern | negated_pattern ; negated_pattern : 'not' negated_pattern | primary_pattern ;

On the other hand, x is not (1 and 2) would mean !(x == 1 && x == 2)

Related Articles