Skip to main content
Stack Overflow
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Detecting an undefined object property

How do I check if an object property in JavaScript is undefined?

Answer*

Draft saved
Draft discarded
Cancel
5
  • @Bergi thank you for your comment. I have corrected my answer. In my defense I can say that currently (as of v.0.10.18) official Node.js documentation says nothing about undefined as a member of global. Also neither console.log(global); nor for (var key in global) { ... } doesn't show undefined as a member of global. But test like 'undefined' in global show the opposite. Commented Sep 11, 2013 at 10:53
  • 4
    It didn't need extra documentation since it's in the EcmaScript spec, which also says that [[Enumerable]] is false :-) Commented Sep 11, 2013 at 11:00
  • 5
    Regarding Minuses of typeof obj.prop == 'undefined', this can be avoided by writing as typeof obj.prop == typeof undefined. This also gives a very nice symmetry. Commented Oct 24, 2014 at 11:01
  • 3
    @hlovdal: That’s totally pointless vs. obj.prop === undefined. Commented Apr 11, 2018 at 21:38
  • 2
    When we are true to the question headline Detecting an undefined property", not true to the (different and much easier) question in the first sentence („check if undefined..."), you answer if ('foo' in o)... your answer is truly the first correct answer here. Pretty much everybody else just answers that sentence. Commented Jun 11, 2018 at 8:46

lang-js

AltStyle によって変換されたページ (->オリジナル) /