Check If An Object Contains A Property
Category: JavaScript
const symbol = Symbol("a");
const obj = { propOne: 1, [symbol]: 2,};
"propOne" in obj; // truesymbol in obj; // true0 in obj; // false
Explanation
The in operator specifically checks whether an object has a specific key. So, instead of accessing the property and checking whether it is undefined, you can better mark your intent by just using in
.