Created
December 29, 2019 22:01
-
-
Save ricealexander/2c71a020971a9bc734f3dcc3b9fa9d39 to your computer and use it in GitHub Desktop.
Ways to determine if an object has a property
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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