I’m trying to validate file extensions in javascript. When I use just one argument in my if statement like this
if(ext!='png' ){
bodyAppend("err","Incorrect file type");
bodyAppend("br","");
}
the code works. But if I add an or statement like this
if(ext!='png' || ext!='jpg'){
bodyAppend("err","Incorrect file type");
bodyAppend("br","");
}
the code does not work and always returns true.
You need to use
&&, not||The problem with the logic:
is that as soon as the extension is one of those options (“png” or “jpg”), it’s NOT the other, so the opposite comparison will always be true. If it’s “png”, it’s NOT “jpg”. If it’s “jpg”, it’s NOT “png”.
I have this problem a lot with SQL queries. When you’re writing out a condition with many comparisons, just try saying it out loud. If the extension is NOT “png” and the extension is NOT “jpg”, then throw an error.