Languages

JavaScript - what is property in hasOwnProperty?

0 points
Asked by:
cory
1786

What is the explanation of hasOwnProperty('propertyName')?

For example:

if (variableName.hasOwnProperty('propertyName')) {
    // some action...
}

Why can't we simply use variableName.propertyName to check if an object contains property with name propertyName?

What is a property in this case?

1 answer
0 points
Answered by:
cory
1786

The hasOwnProperty() returns a boolean value indicating whether the object on which you are calling the method contains a property with the name of the argument.

Practical example

// ONLINE-RUNNER:browser;

var myObject = { key1: 'value1' };

console.log(myObject.hasOwnProperty('key1')); // true
console.log(myObject.hasOwnProperty('key2')); // false

Note:

hasOwnProperty() method doesn't check for the specified property in the object's prototype chain.

 

References

  1. Object.prototype.hasOwnProperty() - JavaScript | MDN
0 comments Add comment
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join