I saw this question, 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 f
All the previous answers are good, but this will be even better. Use dual NOT operators (!!
):
if (!!str) {
// Some code here
}
Or use type casting:
if (Boolean(str)) {
// Code here
}
Both do the same function. Typecast the variable to Boolean, where str
is a variable.
It returns false
for null
, undefined
, 0
, 000
, ""
, false
.
It returns true
for string "0"
and whitespace " "
.