how to check object have attribute or not javascript code example
Example 1: How to tell if an attribute exists on an object
const user = {
name: "Sicrano",
age: 14
}
user.hasOwnProperty('name');
user.hasOwnProperty('age');
user.hasOwnProperty('gender');
user.hasOwnProperty('address');
Example 2: check the properties of an object in javascript
for (var name in object) {
if (object.hasOwnProperty(name)) {
}
}
const hero = {
name: 'Batman'
};
hero.hasOwnProperty('name');
hero.hasOwnProperty('realName');