You can check whether a key exists in an object or not using three approaches,
- Using in operator: You can use the in operator whether a key exists in an object or not
and If you want to check if a key doesn't exist, remember to use parenthesis,
- Using hasOwnProperty method: You can use
hasOwnProperty
to particularly test for properties of the object instance (and not inherited properties)
obj.hasOwnProperty("key"); // true
- Using undefined comparison: If you access a non-existing property from an object, the result is undefined. Let’s compare the properties against undefined to determine the existence of the property.
const user = {
name: "John",
};
console.log(user.name !== undefined); // true
console.log(user.nickName !== undefined); // false