admin管理员组文章数量:1193328
Recently in JavaScript i have picked up the habit of using
if(data !== "")
to check if the data is null || undefined || blank string.
When i try to use
if(data !== null)
//work here
Resharper throws a horrible error at me saying that the
"qualifier can be null or undefined"
I have added a jFiddle just to clarify : JsFiddle
My question is: Is this a ReSharper error or is there something behind this ?
Recently in JavaScript i have picked up the habit of using
if(data !== "")
to check if the data is null || undefined || blank string.
When i try to use
if(data !== null)
//work here
Resharper throws a horrible error at me saying that the
"qualifier can be null or undefined"
I have added a jFiddle just to clarify : JsFiddle
My question is: Is this a ReSharper error or is there something behind this ?
Share Improve this question edited Oct 22, 2017 at 2:01 101 8,9696 gold badges46 silver badges71 bronze badges asked Jun 25, 2014 at 16:20 PogrindisPogrindis 8,0915 gold badges33 silver badges44 bronze badges 8 | Show 3 more comments4 Answers
Reset to default 14First of all, your habit is wrong. Using:
if(data !== "")
Will only check for an empty string. If data
is undefined
or null
, the if block will still execute because !==
checks for equality without performing any type conversions.
Secondly, ReSharper does not have an issue. It's trying to tell you that you may be making a mistake. if(data !== null)
will only check against null. undefined
and an empty string will still return true and cause the block to execute. ReSharper is warning you that you may be making a mistake (because rarely do you ever need to check for just null
).
Just remember that undefined !== null !== ""
. You could attempt several of the shortcuts that are being mentioned but if you really want your code to be thorough, just check all three. If you're worried about code being too long, move the check to a utility method:
function hasValue(var data) {
return (data !== undefined) && (data !== null) && (data !== "");
}
In short, a var
is null
when it's not pointing anywhere.
In the other hand, a var
equal to ""
is a defined var pointing to a variable which contains an empty string. That's essentially different.
[EDIT]
As @jfriend00 correctly points out, null
is a specific value in javascript. The difference would be then that null
is a different value than ""
, which is an empty string and therefor not null.
The correct value for a var
which is not initialized is undefined
.
Null and undefined can be treated in the same way
if (typeof data === "undefined")
will return true if the data is undefined (or null), so
if (typeof data !== "undefined")
will return true if the data has been defined (so is not null), then you can check if it is an empty string,
if (data.length == 0)
will return true if the string is empty.
Is this a ReSharper error or is there something behind this ?
The error might be thrown because of a linter in resharper.
To check if the data is null || undefined || blank string, you could coherce the data value to a boolean (implicitly done by js):
if(!data) //implicit coercion
or do it explicitly:
if(!Boolean(data)){ //same as above
//if data is a falsy value (false, 0, null , undefined, NaN, '' or ""), run
}
You can check the list of truthy and falsy values here
本文标签: javascriptWhy isquotquot is not equal tonullStack Overflow
版权声明:本文标题:javascript - Why is !== "" is not equal to !== null - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738426810a2086172.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
falsey
value, why not just useif (data) {}
– jfriend00 Commented Jun 25, 2014 at 16:22[] != "" -> false
but!![] -> true
. – p.s.w.g Commented Jun 25, 2014 at 16:25falsey
value and testing for!== ""
is no way to test for an empty array either as that doesn't distinguish between an empty array and a full array so I really don't get your point. I wasn't saying thatif (data)
is equivalent to what the OP was using, just that it's probably a better way of testing atruthy/falsey
value which is what it sounds like the OP actually wanted. – jfriend00 Commented Jun 25, 2014 at 16:28