EN
JavaScript - what is property in hasOwnProperty?
1
answers
0
points
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
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
0 comments
Add comment