I am doing a comparison on dates in javascript. In this case date1 is empty "" and I can see the same in firebug. As per the code below, the first alert shouldn’t be called because date1 == "", but for some reason the alert alert(" This is called...."); is invoked. What is wrong here?
if(date1 != null || date1 != ""){
if( (date1 != null || date2 != "") && (date1 < date2)){
alert(" This is called....");
break;
}
else{
alert(" That is called....");
break;
}
}
The above if condition is inside a for loop, hence the break.
I think you mean to be using
&&instead of||in your first comparision to make sure that both conditions are true and I think you have typo where you’re usingdate1instead ofdate2in the second test. Further, you can just useif (date1)to simultaenously rule outnulland""andundefinedand0andNaNand any otherfalseyvalue.I think you want something like this:
If what you’re really trying to do is make sure that
date1anddate2are legal numbers, then I’d suggest you do this:Or, if they’re supposed to be Date objects, then you can test for that: