[SOLVED] Correct order in an if statement

What is the correct order to combine && and || in an if statement?

Hi @Albertos,

That depends on the condition you are trying to check. The if statement will be executed from left to right and you can use parenthesis to make it easier to state. An example:

if( entity.enabled === false || (entity.enabled === true && entity.tags.has('temp') === true) ){
   // do something
}

Here || (OR) has two statements to check:

  • left, a single condition
  • right, a combined condition && (AND) which will evaluate to true only if the whole of it is true.
1 Like

Hello @Leonidas!
So everything inside the parenthesis will be checked together?

Everything inside the parenthesis are counted as a single requirement. If one or the other inside the parenthesis are false, the requirement will not have been met.

2 Likes

Okay clear! Thank you both!

2 Likes

Be aware; the rabbit hole is deep.

:smiley: This will save you some headaches :beers:

1 Like