[英]How can I check if two values are not equal to null with javascript?
I have the following code: 我有以下代码:
var store = window.localStorage;
var a = store.getItem('AccountID');
var b = store.getItem('CityID')
How can I make it so my refreshGrid("Page"); 我该怎么做,以使我的refreshGrid(“ Page”); function only runs if both of these are set to values?
仅当这两个都设置为值时,函数才运行?
If you want to check that they are explicity null
, just compare the values to null
: 如果要检查它们是否显式为
null
,只需将值与null
进行比较:
if(a !== null && b !== null) {
//Neither is null
}
If you are storing values in there that should always evaluate to true
, you can skip the equality checks (if nothing is stored in that particular storage item this will still fail, since null
is falsy): 如果在其中存储的值应始终为
true
,则可以跳过相等性检查(如果该特定存储项中未存储任何内容,则由于null
为假,因此仍然会失败):
if(a && b) {
//Neither is falsy
}
You can use this: 您可以使用此:
var a; var b;
if (a == null && b == null) {
//a and b are null
}
OR 要么
if (!a && !b) {
//a and b are null, undefined, 0 or false
}
For James: alert(null == undefined) // returns true 对于James: alert(null == undefined)//返回true
Hope this helps. 希望这可以帮助。
Use a guard condition at the top of your function: 在函数顶部使用保护条件 :
function myFunction() {
if(a==null || b==null) return;
// ... rest of the code here
}
I think the following code is what your looking for. 我认为以下代码是您想要的。
if(!!a && !!b){
alert("this is not null")
}else{
alert("this is null")
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.