183

Below is a code snippet, where we retrieve a form value. Before further processing check if the value is not null..

var val = document.FileList.hiddenInfo.value;
alert("val is " + val);  // this prints null which is as expected
if (val != null)
{
   alert("value is "+val.length); // this returns 4
}
else
{
   alert("value* is null");
}

Any ideas why it happens so.. ??

1
  • !== instead of != Commented Jul 18, 2019 at 19:54

10 Answers 10

169

this will do the trick for you

if (!!val) {
    alert("this is not null")
} else {
    alert("this is null")
}
Sign up to request clarification or add additional context in comments.

9 Comments

while technically this works, a double not (!!) is a programming faux-pas. It is really the equivalent of if(val), which is much clearer to read. I would stick to that (fyi Ujwal, !!val should technically work in C or C++ also) see here for javascript 'truthiness' values: janosch.woschitz.org/javascript-equality-comparison
how about if(!Boolean(val))
!! is just a double negative, so it's literally saying if(val.exists). I still like the way it looks though!
this will fail if val = 0 since !!0 evaluates to false
It's not a simple double negative. It's a transforming double negative. You are basically transforming the variable to a boolean. There's a lot to say on the subject but suffice it to say that in cases where your variable is 0, false, null, undefined or some such value that resolves to true with one !, you're going to get false for !!. It should be used sparingly if at all.
|
150

There are 3 ways to check for "not null". My recommendation is to use the Strict Not Version.

1. Strict Not Version

if (val !== null) { ... }

The Strict Not Version uses the Strict Equality Comparison Algorithm. The !== operator has faster performance than the != operator, because the Strict Equality Comparison Algorithm doesn't typecast values.

2. Non-strict Not Version

if (val != null) { ... }

The Non-strict Not Version uses the Abstract Equality Comparison Algorithm. The != operator has slower performance than the !== operator, because the Abstract Equality Comparison Algorithm typecasts values.

3. Double Not Version

if (!!val) { ... }

The Double Not Version has faster performance than both the Strict Not Version and the Non-Strict Not Version. However, the !! operator will typecast "falsey" values like 0, '', undefined and NaN into false, which may lead to unexpected results, and it has worse readability because null isn't explicitly stated.

4 Comments

It's very odd. !!val worked for me but val!==null have not. Why is it?
@TheLogicGuy - What is the datatype of val? console.log(typeof val).
number. the code removes "bad" values from the array (null,0 etc). Here is the code:pastebin.com/L7Z1BtuL
@TheLogicGuy - Your actual problem has nothing to do with null. NaN isn't filtering correctly - that will always return true. This should work: !(isNaN(val) && (typeof val !== "undefined")). In Javascript there's a concept known as "Truthy" and "Falsey". The double-not operator !! will remove more than just null. It will convert NaN and undefined into false as well. And that's why your array filters correctly when using the double not-operator !!.
85

It's because val is not null, but contains 'null' as a string.

Try to check with 'null'

if ('null' != val)

For an explanation of when and why this works, see the details below.

2 Comments

should be if (null != val) somehow it was not working with the quotes
Mr.x in the question, null was as string ...your case is probably different
34

Use !== as != will get you into a world of nontransitive JavaScript truth table weirdness.

2 Comments

Unless you want to specifically check for undefined or null. In which case val != null will return true for undefined or null but not for other falsey values like 0.
Imo there's one situation where != is a legitimate use case. And that's exaclty when comparing with null - given you actually don't care whether a missing value ended up as null or as undefined,
9

Check https://softwareengineering.stackexchange.com/a/253723

if(value) {
}

will evaluate to true if value is not:

null
undefined
NaN
empty string ("")
0
false

3 Comments

Just because a value is 'truthy' does not mean its the value you need.
not a good solution if you are checking if a number is assigned (0 will evaluate to false, even though the value is not null). Better check directly if the value is equal to null (value === null)
if (value) means that you're not accepting 0 as a positive value, checking if number is 0 is a very different use case (i mentioned that 0 here is accepted so i can't see how did fit this solution to your case)
8

You should be using the strict not equals comparison operator !== so that if the user inputs "null" then you won't get to the else.

1 Comment

Susan Null would not be happy if her name was not accepted in a data entry form lol
5

This should work fine..

   if(val!= null)
    {
       alert("value is "+val.length); //-- this returns 4
    }
    else
    {
       alert("value* is null");
    }

Comments

5

It is possibly because the value of val is actually the string "null" rather than the value null.

Comments

2

If you want to be able to include 0 as a valid value:

if (!!val || val === 0) { ... }

Comments

0

This will work:

if (val) {
    alert("Not null");
} else {
    alert("Null");
}

1 Comment

Will not work for 0 (zero), undefined, false, "" (empty string) or any falsy value

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.