This if statement should not detect 0; only null or empty strings
If you want to detect all falsey values except zero:
if (!foo && foo !== 0)
So this will detect null
, empty strings, false
, undefined
, etc.
function isNullOrEmptyString(val) {
return (val === null || val === '');
}
console.log({
"isNullOrEmptyString(0)": isNullOrEmptyString(0),
"isNullOrEmptyString('')": isNullOrEmptyString(""),
"isNullOrEmptyString(null)": isNullOrEmptyString(null),
"isNullOrEmptyString('something')": isNullOrEmptyString("something"),
});
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.