Question
This if statement should not detect 0; only null or empty strings
Using JavaScript, how do I NOT detect 0, but otherwise detect null or empty strings?
45 64680
45
Question
Using JavaScript, how do I NOT detect 0, but otherwise detect null or empty strings?
Solution
If you want to detect all falsey values except zero:
if (!foo && foo !== 0)
So this will detect null
, empty strings, false
, undefined
, etc.
Solution
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.