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?
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.
Function:
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.
Meanwhile we can have one function that checks for all 'empties' like null, undefined, '', ' ', {}, []. So I just wrote this.
Use cases and results.
The underscore javascript library http://underscorejs.org/ provides a very useful
_.isEmpty()
function for checking for empty strings and other empty objects.Reference: http://underscorejs.org/#isEmpty
Other very useful underscore functions include:
http://underscorejs.org/#isNull
_.isNull(object)
http://underscorejs.org/#isUndefined
_.isUndefined(value)
http://underscorejs.org/#has
_.has(object, key)
I usually use something like: