![](/img/trans.png)
[英]Javascript check >= 0 not null or undefined or ''
[英]null or undefined check in javascript
我在localStorage
缓存了一个字符串
checkLoop:function(){ //function is hit only if internet is connected
localStorage['key'] = "Some string response from web service";
//JSON Web service could return null, "" (empty response) too
}
仅当有互联网连接时才定义此密钥。 因此,有可能我的函数checkLoop
从未被点击过。 即localStorage从未定义。
稍后我检查一下是否已defined
或为null
因此,进行if(!localStorage['key']){..//TODO..}
这样的检查是否可行?
还是我需要对其进行更多自定义以获得更好的代码?
if(!localStorage['key']){
// Will enter if the value is null\undefined\false\0\""
}
您可能要改用此方法:
if(localStorage['key'] == null){
// only null\undefined.
}
javascript中的虚假值是:
使用typeof
if(typeof localStorage['key'] !== 'undefined'){
// Do Something
}
注意:如果要存储false
或0
值,这很有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.