2

I am getting the following JavaScript error:

'value' is null or not an object

Can someone please let me know what is the best way to check whether an object's value is NULL in JavaScript as I have been using:

if ((pNonUserID !== "") || (pExtUserID !== "")){

Is this correct or is there a better way?

halfer
  • 19,824
  • 17
  • 99
  • 186
tonyf
  • 34,479
  • 49
  • 157
  • 246
  • Your code is testing against an empty string, not `null`. If `pNonUserID = null;` then the expression will evaluate to `true`. But I think the error message is not related to the code you posted. The error is somewhere else. – Felix Kling Mar 30 '11 at 12:30
  • See http://stackoverflow.com/questions/801032/null-object-in-javascript for more info – Vincent Mimoun-Prat Mar 30 '11 at 12:32
  • Felix is right. Worse yet, you are using the identical operator, which checks both value and type. If you used `!=` it would have worked (since it checks the value, and `null==""`). See my answer for more info. – Christian Mar 30 '11 at 12:33

4 Answers4

13

You don't have to do that:

var n=null;

if(n)alert('Not null.'); // not shown
if(!n)alert('Is null.'); // popup is shown

Your error implies otherwise:

var n=null;

alert(n.something); // Error: n is null or not an object.

In the case above, something like this should be used:

if(n)alert(n.something);
Christian
  • 27,509
  • 17
  • 111
  • 155
  • 1
    The implication of your first code example is not correct. `if(!n)alert('Is null.');` will also alert `Is null` if `n == ''` or `n == 0`, and other cases. – Simon Robb Jun 27 '13 at 09:53
3

The !== operator returns true when two variables are not the same object. It doesn't look at the values of the objects at all

To test if something is null:

myVar == null

Your code was testing to see if the variable 'pNonUserId' referred to the same object as "", which can never be true as "" will always be a new instance of the empty string.

As an aside, a test such as:

var n = something();
// do stuff
if (n)
  doSomethingElse();

Is a bad idea. If n was a boolean and false, but you were expecting the if block to test nullify you'll be in for a shock.

Royce
  • 532
  • 3
  • 11
  • I'm going to chime in here with a gripe about testing for null. It should be conceivable (and proper usage) to test for `var n = null; if (n !== null) { doSomething(n); }` but alas, a null Object will hapily evaluate to false above. If it was decided to make null an object, and it was decided that "===" should test for type as well as value, then the above should be valid code. Drives me nuts. – eggmatters Nov 14 '13 at 17:13
0
if (pNonUserID && pExtUserID)
{
   // neither pNonUserId nor pExtUserID are null here
}

Any Javascript variable automatically evaluates to true when it references an object.

What you were doing are comparisons to empty strings, which are not the same as null.

Janick Bernet
  • 20,544
  • 2
  • 29
  • 55
0

null, undefined and empty string is consider as false in conditional statement.

so

if(!n) alert("n is null or undefined or empty string"); 
if(n) alert("n has some value");

therefor, inflagranti suggested condition will work perfectly for you

if(pNonUserID && pExtUserID) { 

}
neeraj
  • 15
  • 1
  • 5