Using JavaScript, how do I NOT detect 0, but otherwise detect null or empty strings?
4 Answers
If you want to detect all falsey values except zero:
if (!foo && foo !== 0)
So this will detect null, empty strings, false, undefined, etc.
2 Comments
SoEzPz
You're almost there, might as well clarify what you mean by 'etc'.
Sarjerao Ghadage
it will not validate '' empty
From your question title:
if( val === null || val == "" )
I can only see that you forgot a = when attempting to strict-equality-compare val with the empty string:
if( val === null || val === "" )
Testing with Firebug:
>>> 0 === null || 0 == ""
true
>>> 0 === null || 0 === ""
false
EDIT: see CMS's comment instead for the explanation.
7 Comments
Hamster
Oh so an empty string is the same as 0 with ==? Odd. Thanks!
Amber
Yes; in fact, that's one of the biggest reasons to prefer
=== over ==. Due to automatic conversion, false == 0 == "", but false !== 0 !== "".alex
Good, good +1. Would a better word for translated be coerced? (just thinking aloud) :)
BoltClock
@alex: Yes, it would. Thanks :)
Christian C. Salvadó
Actually,
0 == "" doesn't coerce to false == false, at the end the compared values will be: 0 == 0. That happens because if one of the operands of the Equals Operator is a Number value, the other will be also converted to Number, and an empty or whitespace only string, type-converts to 0, e.g.: +"" or Number(""). Another example: 0 == { valueOf:function () { return 0;} } evaluates to true, because the object defines an own valueOf method, which is used internally by the ToNumber operation, and we know it doesn't coerce to false, because an object is never falsy. |
function isNullOrEmptyString(val) {
return (val === null || val === '');
}
console.log({
"isNullOrEmptyString(0)": isNullOrEmptyString(0),
"isNullOrEmptyString('')": isNullOrEmptyString(""),
"isNullOrEmptyString(null)": isNullOrEmptyString(null),
"isNullOrEmptyString('something')": isNullOrEmptyString("something"),
});
1 Comment
Michael Freidgeim
The OP asked isNullOrEmptyString , that returns true for “” and null, but false for 0.
I know this might be a too late answer but it might help someone else.
If I understand you correctly you want the below statement to exclude the 0:
if(!value) {
//Do things
}
I think the easiest way to do this is to write the statement like this:
if(!value && value !== 0) {
//Do things
}
I hope this helps.
5 Comments
Michael Freidgeim
The OP wanted to exclude 0, your (value === 0) include 0
Ahmed Sobhy
This statement does exclude 0 from checking "null, false, undefined etc.., it's exactly the same as this one "if (!value&& value!== 0) " except that it's reversed.
Michael Freidgeim
Your answer is wrong, because it should be reversed. Also OP doesn’t ask about false etc
Ahmed Sobhy
Yes, you're right, maybe I wrote it that way because this is the answer "I" would expect in a case like this because I usually check if there is a value then do things with this value not the other way around. @MichaelFreidgeim
Ahmed Sobhy
Updated the answer to match the OP case
val === ""to ensure that val is a string in that second test.