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 ""
?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
- Can php detect if javascript is on or not?
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 usually use some thing like this,
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:
There's nothing representing an empty string in JavaScript. Do a check against either
length
(if you know that the var will always be a string) or against""
Try this