ria
ria

Reputation: 7974

javascript check for not null

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.. ??

Upvotes: 182

Views: 589378

Answers (10)

tim-montague
tim-montague

Reputation: 17362

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.

Upvotes: 148

Feras
Feras

Reputation: 149

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

if(value) {
}

will evaluate to true if value is not:

null
undefined
NaN
empty string ("")
0
false

Upvotes: 8

Arsen Mkrtchyan
Arsen Mkrtchyan

Reputation: 50712

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.

Upvotes: 85

punkrockpolly
punkrockpolly

Reputation: 9908

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

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

Upvotes: 2

Warty
Warty

Reputation: 7395

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.

Upvotes: 8

ScottyUCSD
ScottyUCSD

Reputation: 3776

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

Upvotes: 5

user144390
user144390

Reputation: 305

this will do the trick for you

if (!!val) {
    alert("this is not null")
} else {
    alert("this is null")
}

Upvotes: 168

shajivk
shajivk

Reputation: 570

This will work:

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

Upvotes: 0

Plynx
Plynx

Reputation: 11461

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

Upvotes: 34

vkGunasekaran
vkGunasekaran

Reputation: 6814

This should work fine..

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

Upvotes: 5

Related Questions