Skip to content

Instantly share code, notes, and snippets.

@masautt
Created September 3, 2019 22:31
Show Gist options
  • Save masautt/3c9dcae7d82fc1bd3dc2acf3da270519 to your computer and use it in GitHub Desktop.
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?
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