简体   繁体   English

检查javascript中的空值和空值

[英]Checking for null and empty values in javascript

What is wrong with this if statement? if语句有什么问题? If i pass not empty values i still don't get in to the if block 如果我传递的不是空值,我仍然不会进入if块

Both val1 and val2 have values: val1val2都具有以下值:

 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: val1为空,而val2为一个值:

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

The condition val1 !== null || val1 !== '' 条件val1 !== null || val1 !== '' val1 !== null || val1 !== '' or val2 !== null && val2 !== '' will always be true val1 !== null || val1 !== ''val2 !== null && val2 !== ''将始终为true

Its saying is val1 is not equal to null or empty string. 俗话说val1不等于null或空字符串。 Consider if its null the other part will be automatically true and vice versa. 考虑是否为null ,其他部分将自动为true ,反之亦然。

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() 可以使用every()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 . Val不能为null或为empty

As long as one of those evaluates to true , it will enter the if statement . 只要其中之一评估为true ,它将输入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 . 由于empty string不等于null ,因此null检查为true ,这意味着其中一项检查的评估结果为true ,并将输入if statement If it was vise versa, then the empty string would evaluate to true and again it would enter the if statement . 如果反之亦然,则empty string将评估为true然后再次输入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 . Val可以为null或为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 . Val不能为null 也不能为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. 到&&在if语句中。

 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: 正如其他答案提到的那样,需要将这种表达式(val1 !== null || val1 !== '')更改为(val1 !== null && val1 !== '') ,否则条件将是对于val1 = nullval1 = ''true ,例如:

  • Assume val1 = null , then (val1 !== null || val1 !== '') will be evaluated to (null !== null || null !== '') and this will result in (false || true) that equals true . 假设val1 = null ,则(val1 !== null || val1 !== '')将被评估为(null !== null || null !== '') ,这将导致(false || true)等于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: 现在,对于您的特殊情况,您可以创建一个方法,当您认为变量"defined"时返回true (也可以检查undefined ),然后执行以下操作:

 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 . 只是说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]); } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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