Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
149
rated 0 times [  153] [ 4]  / answers: 1 / hits: 51307  / 10 Years ago, wed, june 25, 2014, 12:00:00

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




enter



I have added a jFiddle just to clarify : JsFiddle



My question is: Is this a ReSharper error or is there something behind this ?


More From » resharper

 Answers
5

First 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 !== );
}

[#70434] Tuesday, June 24, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
leog

Total Points: 225
Total Questions: 113
Total Answers: 118

Location: Oman
Member since Wed, Apr 12, 2023
1 Year ago
;