javascript property contains code example
Example 1: 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');
Example 2: test if property exists javascript
const hero = {
name: 'Batman'
};
hero.hasOwnProperty('name');
hero.hasOwnProperty('realName');
Example 3: object contains property javascript
if (x.hasOwnProperty('y')) {}
if ('y' in x) {}
if (x?.y){}