hasOwnproperty of javascript

Introduction of hasOwnproperty of javascript

In this blog, we will study the hasOwnproperty of javascript. We not only learn about the functionality of this property but will also learn about its syntax as well as an exemplary code for the same. 

The meaning behind hasOwnProperty is that if the object on which you are using the function hasOwnProperty() { [native code] } has a property with the name of the parameter, it returns a boolean value indicating that it does. For instance:

 

 

var x = {

y: 10
};

console.log(x.hasOwnProperty("y")); //true


console.log(x.hasOwnProperty("z")); //false

 

 

It does not, however, examine the object’s prototype chain. When one uses the for…in construct to enumerate an object’s attributes, one should use it.

Every object in JavaScript comes with a number of built-in key-value pairs that include metadata about the object. When you use the for…in construct or loop to iterate through all the key-value pairs for an object, you are also iterating through the key-value pairs for the meta-information.

 

By eliminating these needless iterations through the metadata, the function hasOwnProperty(property) verifies whether the parameter property is a user-provided property in the object. By filters-out, I mean that hasOwnProperty(property) does not check to see if the property is present in the prototype chain of the object, or metadata.

Based on that, it returns a boolean true/false.

 

 

Also Read: rmarkdown figure auto-adjust HTML

 

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *