简体   繁体   中英

Checking for null and empty values in javascript

What is wrong with this if statement? If i pass not empty values i still don't get in to the if block

Both val1 and val2 have values:

 let val1 = 'category' let val2 = 'medical' if ((val1 !== null || val1 !== '') && (val2 !== null || val2 !== '')) { console.log(val1, val2); } 

val1 is empty and val2 has a value:

 let val1 = '' let val2 = 'medical' if ((val1 !== null || val1 !== '') && (val2 !== null || val2 !== '')) { console.log(val1, val2); } 

The condition val1 !== null || val1 !== '' val1 !== null || val1 !== '' or val2 !== null && val2 !== '' will always be true

Its saying is val1 is not equal to null or empty string. Consider if its null the other part will be automatically true and vice versa.

You should use && instead of ||

 var val1 = 'a' var val2 = '' if((val1 !== null && val1 !== '') && (val2 !== null && val2 !== '')){ console.log(val1, val2); } 

A clean code can a be achieved using every() and includes()

 var val1 = 'a' var val2 = '' if([val1, val2].every(x => ![null,''].includes(x))){ console.log(val1, val2); } 

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); } 

You need to change the || to && in your if statement.

 let val1 = 'category' let val2 = 'medical' if ((val1 !== null && val1 !== '') && (val2 !== null && val2 !== '')) { console.log(val1, val2); } 

 let val1 = '' let val2 = 'medical' if ((val1 !== null && val1 !== '') && (val2 !== null && val2 !== '')) { console.log(val1, val2); } 

This should solve your problem.

As other answers have mentioned, this sort of expression (val1 !== null || val1 !== '') needs to be changed to (val1 !== null && val1 !== '') , otherwise the condition will be true for val1 = null or val1 = '' , example:

  • Assume val1 = null , then (val1 !== null || val1 !== '') will be evaluated to (null !== null || null !== '') and this will result in (false || true) that equals true .

Now, for your particular case, you can create a method that returns true when you consider a variable is "defined" (you may check for undefined also) and do something like this:

 function _isDefined(val) { return ![null, undefined, ""].includes(val); } let tests = [ ["category", "medical"], ["category", null], [undefined, "medical"], ["", null], ["something", ""] ]; tests.forEach(function(test) { let [val1, val2] = test; if (_isDefined(val1) && _isDefined(val2)) console.log(val1, val2); }); 
 .as-console {background-color:black !important; color:lime;} .as-console-wrapper {max-height:100% !important; top:0;} 

Just say if . For example:

 var x = ""; var y = null; var z; var argArray = [x,y,z]; function hasValue(arg){ if(arg){ console.log("arg has some value"); } else { console.log("arg has no value and is " + arg); } } for (var i = 0; i < argArray.length; i++) { hasValue(argArray[i]); } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM