Created
September 3, 2019 22:31
-
-
Save masautt/3c9dcae7d82fc1bd3dc2acf3da270519 to your computer and use it in GitHub Desktop.
How do you check if a key exists in an Object in JavaScript?
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
let obj = { key: undefined } | |
// Check if the key is defined (warning: key could exist but set to undefined) | |
obj["key"] !== undefined; | |
//Check if the key is defined in the object (includes if set to undefined) | |
"key" in obj; | |
//Check if the key doesn't exist? | |
!("key" in obj); | |
//If you want to check if the key is inherited or not | |
obj.hasOwnProperty("key"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment