javascript get object properties code example

Example 1: check the properties of an object in javascript

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

Example 2: object keys javascript

const object1 = {
  a: 'somestring',
  b: 42,
  c: false
};

console.log(Object.keys(object1));
// expected output: Array ["a", "b", "c"]

Example 3: javascript list object properties

var person={"first_name":"Bill","last_name":"Jenner"}
//get the person object properties with:
var keys = Object.keys(person); //keys = ["first_name","last_name"]

Example 4: get object name javascript

var a = {
    name:'a',
    'light': 'good',
    'dark' : {
         name: 'dark',
        'black': 'bad',
        'gray' : 'not so bad'
    }
}

// This way you can do

console.log(a.name,a.dark.name);