is there any function like string.isnullorempty() in javascript
I always (thing != undefined || thing != null)?...:...;
check. Is there any method will return bool aft开发者_如何学JAVAer this check in javascript or jquery ?
And how would you add this check in jquery as a function?
if (thing)
{
//your code
}
Is that what you are looking for?
In Javascript, the values null
, undefined
, ""
, 0
, NaN
, and false
are all "falsy" and will fail a conditional.
All other values are "truthy" and will pass a conditional.
Therefore, you can simply write thing ? ... : ...
.
Try this
function SringisEmpty(str) {
str=str.trim();
return (!str || 0 === str.length);
}
As the others here have mentioned, several things evaluate as "falsy" that you might not want to (such as empty strings or zero). The simplest way I've found in JavaScript to check for both null
and undefined
in one statement is:
thing != null
This is using type coercion (double equals instead of triple equals), so undefined values are coerced to null
here, while empty strings, zero, etc. do not.
精彩评论