115

Here is how I tried to mention two conditions if this or this, but it doesn't work.

if (Type == 2 && PageCount == 0) !! (Type == 2 && PageCount == '') {
    PageCount = document.getElementById('<%=hfPageCount.ClientID %>').value;
}

How do I specify multiple conditions in an if statement?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
adilahmed
  • 1,522
  • 3
  • 17
  • 22

9 Answers9

210

Just add them within the main bracket of the if statement like:

if ((Type == 2 && PageCount == 0) || (Type == 2 && PageCount == '')) {
    PageCount = document.getElementById('<%=hfPageCount.ClientID %>').value;
}

Logically, this can be rewritten in a better way too! This has exactly the same meaning:

if (Type == 2 && (PageCount == 0 || PageCount == '')) {
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
AlanFoster
  • 8,156
  • 5
  • 35
  • 52
52

Here is an alternative way to do that.

const conditionsArray = [
    condition1,
    condition2,
    condition3,
]

if (conditionsArray.indexOf(false) === -1) {
    "Do something"
}

Or ES7 (or later):

if (!conditionsArray.includes(false)) {
   "Do something"
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Glen Thompson
  • 9,071
  • 4
  • 54
  • 50
  • 1
    As for me - just too complex. If there is no specific reason to do it this way I would suggest using `&&` operator and, if you do not want to write long/multi-line condition inside `if`, put this under a variable. – AndreyProgr Dec 16 '21 at 11:45
  • 2
    I really like this idea. I ran into a need to add a lot of conditions and this cut down my code a lot. Much more readable. Thanks! – Kenneth Streit Apr 05 '22 at 16:38
  • 1
    Note that this doesn't short-circuit, if you wanted to do that you would need to pass in functions and use `.findIndex` or `.find` – Samathingamajig Aug 05 '22 at 15:33
15

I am currently checking a large number of conditions, which becomes unwieldy using the if statement method beyond say 4 conditions. Just to share a clean looking alternative for future viewers... which scales nicely, I use:

var a = 0;
var b = 0;

a += ("condition 1")? 1 : 0; b += 1;
a += ("condition 2")? 1 : 0; b += 1;
a += ("condition 3")? 1 : 0; b += 1;
a += ("condition 4")? 1 : 0; b += 1;
a += ("condition 5")? 1 : 0; b += 1;
a += ("condition 6")? 1 : 0; b += 1;
// etc etc

if(a == b) {
    //do stuff
}
Edward Taylor
  • 241
  • 2
  • 8
  • 1
    I see no comments that this is unwise practice, so I think it is save to use. Thanks! – CousinCocaine Apr 17 '18 at 19:27
  • 6
    It's worth noting and mentioning that this only works if all conditions must evaluate to true. In other words, this is another way of writing `AND` but not `OR`. – Mark Kramer Nov 27 '19 at 17:04
  • 7
    This requires all conditions to be (potentially unnecessarily) evaluated - in this way it's like a very inefficient alternative to AND. – Shaedo Mar 02 '22 at 03:00
  • The redundancy calls for a loop of some kind (unless the conditions are totally arbitrary). – Peter Mortensen Sep 09 '22 at 14:53
10

The whole if should be enclosed in brackets and the or operator is || and not !!, so

if ((Type == 2 && PageCount == 0) || (Type == 2 && PageCount == '')) { ...
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Fabrizio Calderan
  • 120,726
  • 26
  • 164
  • 177
4

Sometimes you can find tricks to further combine statements.

Like for example:

0 + 0 = 0

and

"" + 0 = 0

so

PageCount == 0
PageCount == ''

can be written like:

PageCount+0 == 0

In JavaScript, 0 is just as good as false. Inverting !, it would turn 0 into true:

!PageCount + 0

For a grand total of:

if (Type == 2 && !PageCount + 0) PageCount = elm.value;
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user40521
  • 1,997
  • 20
  • 8
  • 5
    This is if you want to make your code as short as possible but it goes against many good practices. Keep your code readable. This answer is cryptic and will induce the next person looking at your code into making some mistake. – belvederef Aug 20 '19 at 12:51
4

Wrap them in an extra pair of parentheses and you're good to go.

if((Type == 2 && PageCount == 0) || (Type == 2 && PageCount == ''))
    PageCount = document.getElementById('<%=hfPageCount.ClientID %>').value;
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
3
if((Type == 2 && PageCount == 0) || (Type == 2 && PageCount == '')) {

    PageCount = document.getElementById('<%=hfPageCount.ClientID %>').value;
}

This could be one of possible solutions, so 'or' is ||, not !!.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
vaske
  • 9,332
  • 11
  • 50
  • 69
1

In case you have to many conditions and you want to add them inside only one conditional statement, then you can add the conditions into an array and then construct the conditional statement like so:

let n = 5
let txt = 'hello'

let c = [
  n === 5, 
  n === 4, 
  n === 6,
  txt === 'hello', 
  txt === 'bye'
]

if(c[0] || c[1] || c[2] || c[3] || c[4]){
  document.write('It satisfies ONE or MORE conditions.');
}else{
  document.write('NO conditions have been satisfied.');
}
Gass
  • 7,536
  • 3
  • 37
  • 41
0

OR operator

if ( con1 == True || con2 == True || con3 == True){
     // Statement ...
}

AND operator

if ( con1 == True && con2 == True && con3 == True){
     // Statement ...
}

It works on mine :D

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • Normally you wouldn't use `con1 == True`, you'd just use `con1`. When con1 is something like `x == y`. – Peter Cordes Aug 05 '22 at 09:33
  • Well, given that `True` is not necessarily `true`, maybe it's a variable defined elsewhere? – General Grievance Aug 05 '22 at 13:21
  • Re *"It works on mine"*: Can you be more specific? For example, environment (browser (incl. version) (e.g. [Firefox](https://en.wikipedia.org/wiki/Mozilla_Firefox)), [Node.js](https://en.wikipedia.org/wiki/Node.js) (incl. version), build system (e.g. [Babel](https://en.wikipedia.org/wiki/Babel_(transcompiler))), etc.), supported ECMAScript version, etc.? Please respond by [editing (changing) your answer](https://stackoverflow.com/posts/69293829/edit), not here in comments (********* ***without*** ********** "Edit:", "Update:", or similar - the answer should appear as if it was written today). – Peter Mortensen Sep 09 '22 at 15:14