js check if object has property
snippet in javascript

js check if object has property

user5422

const object1 = new Object();
object1.property1 = 42;

console.log(object1.hasOwnProperty('property1'));
// expected output: true

check the properties of an object in javascript

user3620

for (var name in object) {  
    if (object.hasOwnProperty(name)) { 
        // do something with name                    
    }  
}

// OR

const hero = {
  name: 'Batman'
};

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

javascript does object have property

user9847

var person = {'first_name': 'bill','age':20};

if ( person.hasOwnProperty('first_name') ) {
    //person has a first_name property
}

test if property exists javascript

user6449

const hero = {
  name: 'Batman'
};

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