0

I check for a property on an object but it returns undefined even though it is there. I assume I am testing in the wrong way?

I run this

console.log(self.modules[moduleId]);

and it outputs this:

Object

    composed: Array[2]

    prototypes: Array[2]

    slideshow: Slideshow

        cardFront: false

        currentSlide: 2

(So "slideshow" is an object, an instance of my class "Slideshow".)

I step in one step further to do this:

console.log(self.modules[moduleId].slideshow);

And it returns undefined.

My if-statement looks like this, although above is probably enough to get my issue.

if ( typeof( self.modules[moduleId].slideshow == 'undefined' ) ) {
Matt Welander
  • 8,234
  • 24
  • 88
  • 138
  • 4
    Can you post a jsfiddle of your code? – digitalextremist Feb 04 '14 at 21:54
  • For your debugging... To test for undefined you don't need typeof ... just this: if ( self.modules[moduleId].slideshow == undefined ) { ... } – digitalextremist Feb 04 '14 at 21:58
  • 3
    If `self.modules[moduleId]` really is the object structure you quote, then obviously `self.modules[moduleId].slideshow` **isn't** `undefined`. So something's got lost in the formation of the question. – T.J. Crowder Feb 04 '14 at 21:59
  • 2
    @digitalextremist that is incorrect - in your case, 'undefined' should not be quoted. – Billy Moon Feb 04 '14 at 21:59
  • It's clearly undefined, so you either have a typo somewhere or the object doesn't look like you think it does, and none of those can be solved by just looking at the posted code ? – adeneo Feb 04 '14 at 22:00
  • 1
    YOur brackets are wrong. `if(typeof self.modules[moduleId].slideshow == 'undefined')` would be correct (or `if((typeof self.modules[moduleId].slideshow) == 'undefined')`, but those are unnecessary). ANd, another thing: keep in midn that this only checks `.slideshow`. If `modules[moduleId]` doesn't exist in the first palce, you still get an error - so check that one first. – Johannes H. Feb 04 '14 at 22:00
  • @JohannesH Yes the expression would be less ambiguous written `typeof ( self.modules[moduleId].slideshow == 'undefined' )` the parenthesis is then just an expression, not a parameter of `typeof` which is a keyword not a native function. – axelduch Feb 04 '14 at 22:02
  • 2
    @aduch: It's not that the `()` create "ambiguity," it's that they make the expression wrong. `==` will always return a boolean. `typeof` will always be `"boolean"` with the `()` as in the OP's question, because the `()` make `typeof` apply to the *expression* rather than to `self.modules[moduleId]`. – T.J. Crowder Feb 04 '14 at 22:04
  • @aduch It still operates on the return value that way (which is tpye boolean). check yourself: `typeof (1 == 2)` returns `boolean` – Johannes H. Feb 04 '14 at 22:05
  • Oh, and BTW: javascript offers the `in`operator to test the existence of object properties, which is nicer than the typeof operator in that case. `if ('slideshow' in self.modules[moduleId])` – Johannes H. Feb 04 '14 at 22:07
  • @T.J.Crowder you are right, I did not see that, my bad – axelduch Feb 04 '14 at 22:08
  • @JohannesH.: `in` vs. `typeof ... === "undefined"` is totally a case-by-case thing. :-) – T.J. Crowder Feb 04 '14 at 22:08
  • 1
    @JohannesH. - Good eye, maybe you should add it as an answer, as that seems to be the issue. – adeneo Feb 04 '14 at 22:14
  • glad I could help. I'll add it as an answer for you to accept. – Johannes H. Feb 04 '14 at 22:18

1 Answers1

2

The parantheses in your if-clause are wrong. With the parentheses you have, typeof operates on the value of the comparison expression, which is always a boolean.

Instead, use either

if (typeof self.modules[moduleId].slideshow == 'undefined')

...which will be true if slideshow doesn't exist at all on the object, or if it exists but has the value undefined.

Or make use of the inoperator

if ('slideshow' in self.modules[moduleId])

...which will be true if the object or its prototype has the property, regardless of its value.

Or use hasOwnProperty:

if (self.modules[moduleId].hasOwnProperty('slideshow'))

...which will be true if the object itself (not its prototype) has the property, regardless of its value.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
Johannes H.
  • 5,875
  • 1
  • 20
  • 40