How to specify multiple conditions in an if statement in javascript
JavascriptJavascript Problem Overview
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;
}
Javascript Solutions
Solution 1 - Javascript
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 - Javascript
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 - Javascript
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 - 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 == '')) { ...
Solution 5 - Javascript
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;
Solution 6 - Javascript
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 !!
Solution 7 - Javascript
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;
}
Solution 8 - Javascript
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.');
}
Solution 9 - Javascript
function go(type, pageCount) {
if ((type == 2 && pageCount == 0) || (type == 2 && pageCount == '')) {
pageCount = document.getElementById('<%=hfPageCount.ClientID %>').value;
}
}
Solution 10 - Javascript
OR Operator
if ( con1 == True || con2 == True || con3 == True){
// statement ...
}
AND Operator
if ( con1 == True && con2 == True && con3 == True){
// statement ...
}
Works on mine :D