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
1 Jan 1970

Solution

 68

If you want to detect all falsey values except zero:

if (!foo && foo !== 0) 

So this will detect null, empty strings, false, undefined, etc.

2010-10-11

Solution

 29

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.

2010-10-08