Using JavaScript, how do I NOT detect 0, but otherwise detect null or empty strings?
An empty string is a string instance of zero length, whereas a null string has no value at all. An empty string is represented as "" . It is a character sequence of zero characters. A null string is represented by null .
To check if a string is null or empty in Java, use the == operator. Let's say we have the following strings. String myStr1 = "Jack Sparrow"; String myStr2 = ""; Let us check both the strings now whether they are null or empty.
Use the length property to check if a string is empty, e.g. if (str. length === 0) {} . If the string's length is equal to 0 , then it's empty, otherwise it isn't empty. Copied!
The falsy values in JavaScript are null , undefined , false , 0 , "" (empty string), NaN (not a number). If the val variable stores a null value, the expression before the question mark returns true , and the ternary operator returns 0 .
If you want to detect all falsey values except zero:
if (!foo && foo !== 0)
So this will detect null
, empty strings, false
, undefined
, etc.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With