
JavaScript's hasOwnProperty() Method

In JavaScript, Object.prototype.hasOwnProperty() is a relatively simple method for object manipulation and introspection. Here I'd like to quickly explain hasOwnProperty(), its purpose, and offer a practical example to illustrate its use.
Understanding hasOwnProperty()
hasOwnProperty() is a method used to check whether an object has a property as its own (not inherited from its prototype chain).
Syntax
obj.hasOwnProperty(prop)obj: The object that we want to test.prop: The property name (string) to check for.
Why Use hasOwnProperty()
In JavaScript, objects can often inherit properties from their prototype. hasOwnProperty() helps in determining if a property is actually part of the object itself, rather than inherited, in which case you may want to process it differently.
An Example in Code
Consider an object person with its own properties:
const person = { name: 'Lola', age: 30,};console.log(person.hasOwnProperty('name')); //=> trueconsole.log(person.hasOwnProperty('toString')); //=> false- '
name' is the object's own property, sohasOwnProperty('name')returnstrue. - '
toString', however, is inherited from the object's prototype, sohasOwnProperty('toString')returnsfalse.
Key Takeaways
Use hasOwnProperty() to check for an object's own properties. This can be essential for iterating over an object's properties, especially to distinguish between its own properties and those it has inherited from the prototype (or elsewhere).
Wrapping up
Object.prototype.hasOwnProperty() is a simple yet powerful method in JavaScript for object property checks. It ensures that you are working with properties that belong to the object itself, not properties inherited from its prototype, which is crucial for accurate data manipulation and validation in JavaScript applications.
Related Articles

3Sum Closest in JavaScript: Sorting and Two Pointers. 
JavaScript String Manipulation: substring() vs. substr(). JavaScript String Manipulation:
substring()vs.substr()
Backtracking Decision Trees: Solving 'Combination Sum'. Backtracking Decision Trees: Solving 'Combination Sum'

Appending and Prepending Items to an Array. Appending and Prepending Items to an Array

Exploring the Liquid Templating Language. Exploring the Liquid Templating Language

String to Integer (atoi): Decoding Strings in JavaScript. String to Integer (atoi): Decoding Strings in JavaScript

Primitive vs. Reference Types in JavaScript. Primitive vs. Reference Types in JavaScript

Pure Functions in JavaScript. Pure Functions in JavaScript

Extends and super in JavaScript Classes. extendsandsuperin JavaScript Classes
If Not Internet Explorer Conditional HTML. If Not Internet Explorer Conditional HTML

Sorting Objects in JavaScript. Sorting Objects in JavaScript

Understanding Media Queries in CSS. Understanding Media Queries in CSS