47

Using JavaScript, how do I NOT detect 0, but otherwise detect null or empty strings?

2
  • use val === "" to ensure that val is a string in that second test. Commented Oct 8, 2010 at 5:12
  • 3
    I don't know about you, but it's easier for me to logically follow this sentence: "How do I detect null or empty strings, but not 0". The only reason I mention this is because structuring code in the order you'd say it can sometimes make it easier to implement. Commented Oct 8, 2010 at 7:10

4 Answers 4

72

If you want to detect all falsey values except zero:

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

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

Sign up to request clarification or add additional context in comments.

2 Comments

You're almost there, might as well clarify what you mean by 'etc'.
it will not validate '' empty
31

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

Oh so an empty string is the same as 0 with ==? Odd. Thanks!
Yes; in fact, that's one of the biggest reasons to prefer === over ==. Due to automatic conversion, false == 0 == "", but false !== 0 !== "".
Good, good +1. Would a better word for translated be coerced? (just thinking aloud) :)
@alex: Yes, it would. Thanks :)
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.
|
1

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

The OP asked isNullOrEmptyString , that returns true for “” and null, but false for 0.
-1

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

The OP wanted to exclude 0, your (value === 0) include 0
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.
Your answer is wrong, because it should be reversed. Also OP doesn’t ask about false etc
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
Updated the answer to match the OP case

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.