Skip to content

Instantly share code, notes, and snippets.

@ricealexander
Created December 29, 2019 22:01
Show Gist options
  • Select an option

  • Save ricealexander/2c71a020971a9bc734f3dcc3b9fa9d39 to your computer and use it in GitHub Desktop.

Select an option

Save ricealexander/2c71a020971a9bc734f3dcc3b9fa9d39 to your computer and use it in GitHub Desktop.
Ways to determine if an object has a property
const colors = {
seashell: '#FFF5EE',
cornsilk: '#FFF8DC',
lemonchiffon: '#FFFACD',
floralwhite: '#FFFAF0',
snow: '#FFFAFA',
lightyellow: '#FFFFE0',
ivory: '#FFFFF0',
}
const target = 'ivory'
// double negation of bracket notation
!!colors[target]
// cast bracket notation as Boolean
Boolean(colors[target])
// Object.prototype.hasOwnProperty
colors.hasOwnProperty(target)
// property `in` object
target in colors
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment