We are going to explore JavaScript’s hasOwnProperty() method in this article. hasOwnProperty() method checks whether the property on an object belongs to the mentioned object or not. If the property belongs to the mentioned object then it will return true else it will return false.

Object’s member inherit the hasOwnProperty() method. This function is used to find out whether the object has the mentioned property directly related to the object.

JavaScript hasOwnProperty() Syntax

obj.hasOwnProperty(property)

property: Pass the string name or symbol to check whether it belongs to the object.

Example

let user = {
name: 'Sathish Kumar',
email: 'ramsatt@gmail.com',
city: 'Chennai',
Country: 'India'
}

console.log(user.hasOwnProperty('name'));
//It returns true

console.log(user.hasOwnProperty('mobile'));
//It returns false
//Because the user object does not have the property mobile