Here is how I mention two conditions if this or this
if (Type == 2 && PageCount == 0) || (Type == 2 && PageCount == '')
PageCount= document.getElementById('<%=hfPageCount.ClientID %>').value;
}
This question is related to
javascript
the whole if
should be enclosed in brackets and the or
operator is ||
an not !!
, so
if ((Type == 2 && PageCount == 0) || (Type == 2 && PageCount == '')) { ...
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 !!
function go(type, pageCount) {
if ((type == 2 && pageCount == 0) || (type == 2 && pageCount == '')) {
pageCount = document.getElementById('<%=hfPageCount.ClientID %>').value;
}
}
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
}
Wrap them in an extra pair of parens and you're good to go.
if((Type == 2 && PageCount == 0) || (Type == 2 && PageCount == ''))
PageCount= document.getElementById('<%=hfPageCount.ClientID %>').value;
}
Here is an alternative way to do that.
const conditionsArray = [
condition1,
condition2,
condition3,
]
if (conditionsArray.indexOf(false) === -1) {
"do somthing"
}
Or ES6
if (!conditionsArray.includes(false)) {
"do somthing"
}
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;
Source: Stackoverflow.com