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.

0

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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: That seems to be an edge case. More info here. 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.
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: 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.
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 :)

1 Comment

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 :)
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' ) { .... }

1 Comment

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

2 Comments

typeof always returns a string. so (typeof MyObject[TheDate] === 'undefined') can be written as (typeof MyObject[TheDate] == 'undefined') with no worries.
@Angel typeof does, yes, but he didn't use typeof in the first place.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.