I saw this thread, but I didn't see a JavaScript specific example. Is there a simple string.Empty
available in JavaScript, or is it just a case of checking for ""
?
Join them; it only takes a minute:
|
|||||
|
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:
For checking if a string is blank or contains only white-space:
|
|||||||||||||||||||||
|
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 |
|||||||||||||||||||||
|
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:
|
|||||||||||||||||||||
|
I use :
|
|||||
|
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 could some how end up containing an integer 0 value, then that would indeed be one of those intention-clarifying situations. |
|||||||||||||||||
|
There's nothing representing an empty string in JavaScript. Do a check against either |
|||
|
Several methods:
|
|||||||||||||||||
|
you could also go with regexps:
Checks for strings that are either empty or filled with whitespace. |
|||||||||
|
Try:
|
||||
|
A lot of answers, and a lot of different possibilities! Without a doubt for quick and simple implementation the winner is: However, as many other examples are available. The best functional method to go about this, I would suggest:
|
|||||||||
|
Also, in case you consider a whitespace filled string as "empty". You can test it with this Regex:
|
|||
|
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.). |
|||||
|
If one needs to detect not only empty but also blank strings, I'll add to Goral's answer:
|
|||
|
I use a combination, fastest checks are first.
|
|||||
|
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. |
||||
|
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 usually use some thing like this,
|
|||||||||||||||||
|
|
||||
|
I usually use something like:
|
|||
|
this is also a generic way to check if field is empty. |
|||
|
str.value.length == 0 |
|||||
|
Don't assume that the variable you check is a string. Don't assume that if this var has a length, then it's a string. The thing is: think carefully about what your app must do and can accept. Build something robust. If your method / function should only process a non empty string then test if the argument is a non empty string and don't do some 'trick'. As an example of something that will explode if you follow some advices here not carefully.
So, I'd stick with
|
|||
|
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.
|
|||
|
I prefer to use not blank test instead of blank
|
|||
|
You should always check for the type too, since JavaScript is a duck typed language, so you may not know when and how the data changed in the middle of the process. So, here's the better solution:
|
|||
|
The underscore javascript library http://underscorejs.org/ provides a very useful Reference: http://underscorejs.org/#isEmpty
Other very useful underscore functions include: |
|||
|
protected by Doorknob Nov 1 '13 at 12:57
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?