check if field exists in object javascript 1

check if field exists in object javascript

const hero = {
  name: 'Batman'
};

hero.hasOwnProperty('name');     // => true
hero.hasOwnProperty('realName'); // => false

Here is what the above code is Doing:
1. We create an object called hero.
2. We check if the hero object has a property called name.
3. We check if the hero object has a property called realName.

The first check returns true because the hero object has a property called name.
The second check returns false because the hero object does not have a property called realName.

Instructions
1.
Check if the hero object has a property called realName.

2.
Check if the hero object has a property called city.

Similar Posts