Oliver Jumpertz

Check If An Object Contains A Property

Category: JavaScript

Share this snippet to:

const symbol = Symbol("a");
const obj = {
propOne: 1,
[symbol]: 2,
};
"propOne" in obj; // true
symbol in obj; // true
0 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.


Share this snippet to: