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?
undefined
variable, 3 can also be done asif (foo.bar !== undefined) {
. – Mohsen