Skip to content

Instantly share code, notes, and snippets.

@batazo
Created July 3, 2022 18:34
Show Gist options
  • Select an option

  • Save batazo/cd779d09dbc2638bef41eddce60ed54f to your computer and use it in GitHub Desktop.

Select an option

Save batazo/cd779d09dbc2638bef41eddce60ed54f to your computer and use it in GitHub Desktop.
Object.hasOwn()
const person = Object.create(null);
person.age = 21;
if (Object.hasOwn(person, 'age')) {
console.log(person.age); // true - works regardless of how the object was created
}
if (person.hasOwnProperty('age')){ // throws error - person.hasOwnProperty is not a function
console.log('hasOwnProperty: ', person.age);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment