You want to use && and not ||, because what you are currently saying is:
Val cannot be null or it cannot be empty.
As long as one of those evaluates to true, it will enter the if statement. Since an empty string does not equal null, the null check is true which means one of the checks evaluated to true and would enter the if statement. If it was vise versa, then the empty string would evaluate to true and again it would enter the if statement.
This would make more sense if you were checking for equality and would then read as follows:
Val can be null or it can be empty.
However, that isn't what you are looking for, so what you want to say is:
Val cannot be null AND it cannot be empty.
- When testing a single variable to not equal multiple values use an
&& check.
- When testing a single variable to equal one of multiple values use an
|| check.
let val1 = 'category'
let val2 = 'medical'
if ((val1 !== null && val1 !== '') && (val2 !== null && val2 !== '')) {
console.log(val1, val2);
}
val1 = ''
if ((val1 !== null && val1 !== '') && (val2 !== null && val2 !== '')) {
console.log(val1, val2);
}
||'s should be&&'s[val1, val2]do you expect it to enter theif. Because actually, forval1="foo", val2="bar"I'm getting the console output.truebecause you can't have a variable that is an empty string andnullat the same time.