Search code examples
javascriptobjectecmascript-5

print properties and values of an object at the same time-Javascript


Assuming we have the following Javascript Object

var t={name:"John",age:34,zip:"82900"}

If I use the following code to print all properties of that object:

for(var x in t){
console.log(t[x]);
}

I will get John, 34, 82900.Now my question is how to to print each propertie's name for example to print age,34 name,John that object might have more properties than i wrote above since the user can add its own properties inside that object


Solution

  • var t = { name : "John", age : 34, zip : "82900" };
    
    for(var x in t){
      console.log(x, t[x]);
    }