How to specify multiple conditions in an if statement in javascript

Solution 1:

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 == '')) {

Solution 2:

Here is an alternative way to do that.

const conditionsArray = [
    condition1, 
    condition2,
    condition3,
]

if (conditionsArray.indexOf(false) === -1) {
    "do somthing"
}

Or ES7+

if (!conditionsArray.includes(false)) {
   "do somthing"
}

Solution 3:

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
}

Solution 4:

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

if ((Type == 2 && PageCount == 0) || (Type == 2 && PageCount == '')) { ...

Solution 5:

Sometimes you can find tricks to further combine statments.

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;