-2

I'm new to javascript and I'd just like a simple explanation of how to use "if" with multiple conditions in javascript.

I've seen stuff like this: if (condition1 && condition2) and if (condition1) || (condition2)

To my understanding, && is basically "and", and || is basically "or". Please correct me if I'm wrong.

But would you use these if you wanted to test more than 2 conditions? And are there any other marks besides && and || ? Also, if you were to have a much larger list of conditions. Let's say 10 or 20, then is there a better/proper way to test them other than sticking them all in if() seperated by && and || ?

Dylan Madigan
  • 31
  • 1
  • 1
  • 2
  • `if(condition1 || condition2 || condition3)`, `if(condition1 && condition2 && condition3)`. Checking multiple efficiently depends on the usecase. – Andrew Li Oct 03 '16 at 02:45
  • 2
    Possible duplicate of [Check variable equality against a list of values](http://stackoverflow.com/questions/4728144/check-variable-equality-against-a-list-of-values) and [Concise way to compare against multiple values](http://stackoverflow.com/questions/13737091/concise-way-to-compare-against-multiple-values). – Andrew Li Oct 03 '16 at 02:49
  • Guy your question is duplicated, please check: [https://stackoverflow.com/questions/13737091/concise-way-to-compare-against-multiple-values](Check variable equality against a list of values) – rflmyk Jan 25 '18 at 11:43

1 Answers1

2

If you want more test conditions, you can just add more "&&"'s and "||"'s. You can also group test conditions into parentheses so it'll work like order of operations in mathematics. For example...

if((x && y) || z) {
//do this;
}

Honestly, for super-long conditions, just format the code properly. Start new lines on occasion, be sure to indent to show that you're still adding conditions, and again, make sure to properly use your parentheses.