1const object1 = new Object();
2object1.property1 = 42;
3
4console.log(object1.hasOwnProperty('property1'));
5// expected output: true
1for (var name in object) {
2 if (object.hasOwnProperty(name)) {
3 // do something with name
4 }
5}
6
7// OR
8
9const hero = {
10 name: 'Batman'
11};
12
13hero.hasOwnProperty('name'); // => true
14hero.hasOwnProperty('realName'); // => false
1const hero = {
2 name: 'Batman'
3};
4
5hero.hasOwnProperty('name'); // => true
6hero.hasOwnProperty('realName'); // => false
1var person = {'first_name': 'bill','age':20};
2
3if ( person.hasOwnProperty('first_name') ) {
4 //person has a first_name property
5}
6