I am a complete novice at Javascript and having tried every possible combination of Boolean expressions I am left with the following code. Why does the following JS boolean logic fail for everything other than where effectiveDateSelected = true and throughDateSelected = false
// initialisation
var effectiveDateSelected = new Boolean(false);
var throughDateSelected = new Boolean(false);
// values read in from web page
effectiveDateSelected = ...
throughDateSelected = ...
// the logical expression
if ((effectiveDateSelected) && !(throughDateSelected)) {
reportNum = 1;
alert("1 reportNum=" + reportNum);
}
if (!(effectedDateSelected) && (throughDateSelected)) {
reportNum = 2;
alert("2 reportNum=" + reportNum);
}
if (((effectedDateSelected) && (throughDateSelected)) ||
(!(effectedDateSelected) && !(throughDateSelected))) {
reportNum = 3;
alert("3 reportNum=" + reportNum);
}
new Boolean()is always truthy as it’s an object, you want to use boolean primitives likefalseandtruedirectly.You can see this when converting a boolean object to primitive: