0

Learning JavaScript I ran across two methods to find out whether object has defined property.

Method A
if ('propertyName' in object) { }
Method B
if (object.propertyName) { }

Using console.log() I found out that Method A yields boolean value whereas Method B yields either undefined or value of the property if defined.

Are there any other differences between these two methods or reasons I should prefer using one over another? If there are no differences I would prefer using Method B since there is no need for using string.

Superian007
  • 395
  • 7
  • 25
  • 1
    Do you need to differentiate between *falsey* values like `undefined`, `false`, `null` etc? That's where a plain `if (o.p)` differs significantly. – deceze Jul 20 '16 at 10:04
  • Duplicate of http://stackoverflow.com/questions/135448/how-do-i-check-if-an-object-has-a-property-in-javascript? – M.M Jul 20 '16 at 10:05

0 Answers0