Skip to main content

Posts

Showing posts with the label hasownproperty

JavaScript: Is a member defined?

It seems to me that there are four different ways I can determine whether a given object (e.g. foo ) has a given property (e.g. bar ) defined: if (foo.hasOwnProperty(bar)) { if ('bar' in foo) { if (typeof foo.bar !== 'undefined') { if (foo.bar === undefined) { To determine if there is a property named " bar " in the object foo , are all three of those statements equivalent? Are there any sublte semantics I don't know that makes any of these three statements different? Source: Tips4all