List All Properties of a Javascript Object

September 19, 2017

You can use for loop to list all properties and methods in object

function Product(name){
    this.name=name,
    this.category="food",
    this.price=10,
    this.getDiscount=function(){
      return this.price*5/100
    }
}

var p= new Product("Burger")

for (var prop in p){
    console.log(prop+" : "+ p[prop])
}

Output will be
name : Burger
category : food
price : 10
getDiscount :function () {
return this.price * 5 / 100;
}