DEV Community

Discussion on: Check if key exists in Dictionary/Object in Python vs Javascript

Collapse
lukaszahradnik profile image
Lukáš Zahradník • Edited on

The JavaScript version is wrong. The object can contain property that is falsey and you code would evaluate as if the object doesn't contain that property.

The right way would be using in operator, hasOwnProperty method or similar.

if ('platform' in obj) {
  // ...
}

if (obj.hasOwnProperty('platform')) {
  // ...
}