Check Given Property is Own Property of an Object in Javascript

Last Updated: September 19, 2017

You can check the given property is an own property of an object. You can use the hasOwnProperty() of the object

function Product(name){
    this.name=name
}

Product.prototype.price=1
Product.prototype.getDiscount=function(){
    return this.price*5/100
}

var product= new Product('Fanta')

console.log("name is a property of product "+product.hasOwnProperty("name"))
console.log("price is a property of product "+product.hasOwnProperty("price"))

You can have the following output
>name is a property of product true
>price is a property of product false