1

I have a string like this:

var TheDate = "6.14.2012";

and I have an object MyObject that has properties that may match that string. However, when I write this:

if (MyObject[TheDate] === null || MyObject[TheDate] === 'undefined') { .... }

the conditions never seem to evaluate to true even though I know the property doesn't exist.

What am I missing??

Thanks.

frenchie
  • 51,731
  • 109
  • 304
  • 510

4 Answers4

3

Existence of Own Property

If you want to check for the existence of a property use .hasownProperty()...

if (MyObject.hasOwnProperty(TheDate)) {

Existence of Own Or Inherited Property

If the property may be inherited, to test for existence use the in operator...

if (TheDate in MyObject) {

null or undefined value test for Own Or Inherited Property

If it's not existence, but rather a value test, and you want to test for null or undefined, then use this...

if (MyObject[TheDate] == null) {

This will check for both null and undefined.

  • hasOwnProperty doesn't seem to work well in IE8; not personally tested but seems well documented. I've avoided using it so far. How can this work safely in IE8? – frenchie Jun 14 '12 at 14:32
  • @frenchie: That seems to be an edge case. [More info here](http://stackoverflow.com/questions/3705383/ie8-bug-in-for-in-javascript-statement). But if you actually want to test for `null` or `undefined`, see the 3rd solution. If inherited property check is alright *(I'm guessing it probably is)*, see the 2nd solution using the `in` operator. –  Jun 14 '12 at 14:35
  • Ok, I decided to not use the hasOwnProperty method and simply remove the quotes and it works. Is .hasOwnProperty going to work better than if (MyObject[TheDate] === null || MyObject[TheDate] === undefined) ? – frenchie Jun 14 '12 at 14:45
  • @frenchie: It all depends on what you're actually wanting to test. If you want to test to see if the property exists, you must use one of the first two solutions I gave. If you want to test for `null` or `undefined` values, then use the `MyObject[TheDate] == null`. There's no reason to test for them separately. This tests for both at the same time, because of the way the `==` operator works. –  Jun 14 '12 at 14:46
1

The problem is with the quotes around undefined ;)

Change

if (MyObject[TheDate] === null || MyObject[TheDate] === 'undefined') { .... }

to

if (MyObject[TheDate] === null || MyObject[TheDate] === undefined) { .... }

It should work :)

Tamil
  • 5,260
  • 9
  • 40
  • 61
  • It is the one of the trusted way to do, provided you are operating with the property of an object :) else typeof x == "undefined" is good :) – Tamil Jun 14 '12 at 14:39
0

If you want to use the string "undefined" rather than the undefined object, the way to do this is to check the "typeof", which returns a string. In this case, if you're looking to test if it's set to "undefined":

if ( MyObject[TheDate] === null || typeof MyObject[TheDate] === 'undefined' ) { .... }
buley
  • 28,032
  • 17
  • 85
  • 106
  • Sorry, you caught me mid-edit. The correct syntax is the above, which checks the object. Checking typeof of a string will always return a string. – buley Jun 14 '12 at 14:31
0

Try the following

if ( (MyObject[TheDate] === null) || (typeof MyObject[TheDate] === 'undefined') { 
 .... 
}

You were checking whether your property contains the String 'undefined' and not whether its value is currently undefined.

Sirko
  • 72,589
  • 19
  • 149
  • 183
  • typeof always returns a string. so (typeof MyObject[TheDate] === 'undefined') can be written as (typeof MyObject[TheDate] == 'undefined') with no worries. – Angel Jun 14 '12 at 14:48
  • @Angel `typeof` does, yes, but he didn't use `typeof` in the first place. – Sirko Jun 14 '12 at 14:50