I saw this thread, but I didn't see a JavaScript specific example. Is there a simple string.Empty
in JavaScript, or is it just checking for ""
?
|
||||
|
If you just want to check whether there's any value, you can do
If you need to check specifically for an empty string over null, I would think checking against |
|||||||||||||||||||||
|
For checking if a string is empty, null or undefined I use:
For checking if a string is blank, null or undefined I use:
|
|||||||||||||||||||
|
If you need to make sure that the string is not just a bunch of empty spaces (I'm assuming this is for form validation) you need to do a replace on the spaces.
|
|||||||||||||||||||||
|
The closest thing you can get to str.Empty (with the precondition that str is a String) is:
|
|||||||
|
All the above are good but this will be even better. use
or use type casting:
Both do the same function, type cast the variable to boolean, where |
|||||||||||
|
I would not worry too much about the most efficient method. Use what is most clear to your intention. For me that's usually EDIT: per comment from Constantin, if strVar some how ended up containing an integer 0 value, then |
|||||
|
you could also go with regexps:
Checks for strings that are either empty or filled with whitespace. |
|||
|
There's nothing representing an empty string in JavaScript. Do a check against either |
|||
|
I use :
|
|||||||
|
I use a combination, fastest checks are first.
|
|||
|
I have not noticed an answer that takes into account the possibility of null characters in a string. For example, if we have a null character string:
To test its nullness one could do something like this:
It works on a null string, and on an empty string and it is accessible for all strings. In addition, it could be expanded to contain other JavaScript empty or whitespace characters (i.e. nonbreaking space, byte order mark, line/paragraph separator, etc.). |
|||
|
I usually use something like:
|
|||
|
Ignoring whitespace strings, you could use this to check for null, empty and undefined :
Concise and it works for undefined properties, although it's not the most readable. |
||||
|
this is also a generic way to check if field is empty. |
|||
|
Also, in case you consider a whitespace filled string as "empty". You can test it with this Regex:
|
|||
|
It's a good idea too to check that you are not trying to pass an undefined term.
I usually run into the case where I want to do something when a string attribute for an object instance is not empty. Which is fine, except that attribute is not always present. |
|||
|
An alternative way, but I believe bdukes's answer is best.
|
|||
|
All these answers are nice. But I cannot be sure that variable is a string, doesn't contains only spaces (this is important for me), and can contain '0' (string). My version:
Sample on jsfiddle. |
|||
|
I did some research what happens if you pass a non-string and non-empty/null value to a tester function. As many knows, (0 == "") is true in javascript, but since 0 is a value and not empty or null, you may want to test for it. The following two functions return true only for undefined, null, empty/whitespace values and false for everything else, such as numbers, boolean, objects, expressions etc.
More complicated examples exists, but these are simple and give consistent results. There is no need to test for undefined, since it's included in (value == null) check. You may also mimic C# behaviour by adding them to String like this:
You do not want to put it in Strings prototype, because if the instance of the String-class is null, it will error:
I tested with the following value array. You can loop it through to test your functions if in doubt.
|
|||
|
I prefer to use not blank test instead of blank
|
|||
|