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?
All the above are good but this will be even better. use
!!
(not not) operator.or use type casting:
Both do the same function, type cast the variable to boolean, where
str
is a variable.Returns
false
fornull,undefined,0,000,"",false
.Returns
true
for string "0" and whitespace " ".You can use lodash : _.isEmpty(value).
It covers a lot of cases like
{}
,''
,null
,undefined
etc.But it always returns
true
forNumber
type of Javascript Primitive Data Types like_.isEmpty(10)
or_.isEmpty(Number.MAX_VALUE)
both returnstrue
.var a;
existtrim out the
false spaces
in the value, then test foremptiness
this is also a generic way to check if field is empty.
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
""
is your best bet, using the===
operator (so that you know that it is, in fact, a string you're comparing against).Try: