javascript check for not null

Javascript

Javascript Problem Overview


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

Javascript Solutions


Solution 1 - Javascript

this will do the trick for you

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

Solution 2 - Javascript

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" http://www.ecma-international.org/ecma-262/5.1/#sec-11.9.6. The !== 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 version uses the "Abstract Equality Comparison Algorithm" http://www.ecma-international.org/ecma-262/5.1/#sec-11.9.3. The != 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 != (https://jsperf.com/tfm-not-null/6). However, it will typecast "Falsey" values like undefined and NaN into False (http://www.ecma-international.org/ecma-262/5.1/#sec-9.2) which may lead to unexpected results, and it has worse readability because null isn't explicitly stated.

Solution 3 - Javascript

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.

Solution 4 - Javascript

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

Solution 5 - Javascript

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.

Solution 6 - Javascript

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

if(value) {
}

will evaluate to true if value is not:

null
undefined
NaN
empty string ("")
0
false

Solution 7 - Javascript

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

Solution 8 - Javascript

This should work fine..

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

Solution 9 - Javascript

This will work:

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

Solution 10 - Javascript

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

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionriaView Question on Stackoverflow
Solution 1 - Javascriptuser144390View Answer on Stackoverflow
Solution 2 - Javascripttim-montagueView Answer on Stackoverflow
Solution 3 - JavascriptArsen MkrtchyanView Answer on Stackoverflow
Solution 4 - JavascriptPlynxView Answer on Stackoverflow
Solution 5 - JavascriptWartyView Answer on Stackoverflow
Solution 6 - JavascriptFerasView Answer on Stackoverflow
Solution 7 - JavascriptScottyUCSDView Answer on Stackoverflow
Solution 8 - JavascriptvkGunasekaranView Answer on Stackoverflow
Solution 9 - JavascriptshajivkView Answer on Stackoverflow
Solution 10 - JavascriptpunkrockpollyView Answer on Stackoverflow