Javascript Tutorial
Javascript Tutorial Introduction to Javascript JavaScript Code Editors & IDE JavaScript Hello World Example Javascript Syntax and Rules syntax_rules javascript_identifiers JavaScript Keywords & Reserved Words javascript_variables JavaScript Const JavaScript let vs var vs const Data Types in JavaScript JavaScript String Template Literals & String interpolation in JavaScript Tagged Templates in JavaScript String to Number in JavaScript Number Data Type in JavaScript NaN in JavaScript JavaScript Number Min & Max & Safe Values JavaScript EPSILON & Floating point precision Infinity in JavaScript JavaScript Bigint BigInt Vs Number in JavaScript Boolean Data Type in JavaScript Undefined in JavaScript Null in JavaScript Null vs Undefined in JavaScript JavaScript Operators Arithmetic Operators in JavaScript Unary plus & minus operators in JavaScript Increment & Decrement Operators in JavaScript Comparison or Relational operators in JavaScript Strict Equality (==) Loose Equality (===) in JavaScript Ternary Conditional Operator in JavaScript Logical Operators in JavaScript Bitwise Operators in JavaScript Assignment Operators in JavaScript Nullish Coalescing Operator in JavaScript Comma Operator in JavaScript Typeof JavaScript Operator Precedence in JavaScript JavaScript if, else & nested if statement Switch Statement in JavaScript While & Do While Loops in JavaScript For Loop in JavaScript Break statement in JavaScript Continue Statement in JavaScript Arrays in JavaScript Array Constructor in Javascript Sparse Array Vs Dense Array in JavaScript How to merge Arrays in JavaScript Array Methods in JavaScript Functions in JavaScript Function Parameters & Arguments in JavaScript JavaScript Default Parameters Pass by Value and Pass by Reference in Javascript Function Expression in Javascript Nested Functions in JavaScript Immediately-invoked Function Expressions (IIFE) JavaScript Callback Functions Arrow Functions in JavaScript Arguments Object In JavaScript Rest Parameters in JavaScript Objects in Javascript Create Objects in JavaScript JavaScript Object Properties Computed Property Names in JavaScript Object Literal in JavaScript Constructor Function & New Operator in JavaScript Delete Operator in JavaScript hasOwnProperty in JavaScript Using Getters and Setters in Javascript DefineProperty in JavaScript JavaScript Property Descriptors Enumerable, Writable & Configurable Object Destructuring in JavaScript Variable Scope in JavaScript Hoisting in JavaScript Lexical Scope & Closures in JavaScript This in JavaScript Global Object, Window & Globalthis in JavaScript Call function in Javascript Prototype In Javascript Prototype Inheritance in JavaScript Instanceof Operator in JavaScript Spread Operator in JavaScript

hasOwnProperty in JavaScript

The hasOwnProperty returns true if the property is its own property and not inherited.

Syntax of hasOwnProperty

        
object.hasOwnProperty(prop)
 
 
                
        
    

Where prop is the name of the property to check.

In the example below, we define prop1 in the obj. Hence the hasOwnProperty returns true. But prop2 does not belong to obj, Hence it returns false.

        
const obj = {
    prop1: '1'
};
 
obj.hasOwnProperty('prop1');  // true
obj.hasOwnProperty('prop2');  // false. Does not exists
 
                
        
    

Own vs inherited Property

The hasOwnProperty returns true only if the property is its own property. For Example, toString in the following code is the inherited property hence returns false.

        
var str = new String("Hello")
 
console.log(str.toString())     //Works
 
console.log(str.hasOwnProperty('toString'))   //false
 
 
                
        
    

The object must inherit from Object

The hasOwnProperty method belongs to the Object.prototype. Hence it works only our object inherits from it.

When we create an object using object literal or constructor function, it always inherits from the Object.

But, you can also create an object without settings it prototype as shown below. In that case hasOwnProperty will not work

        
newObj = Object.create(null)
newObj.prop1="";
 
newObj.hasOwnProperty('prop1')  //Uncaught TypeError: newObj.hasOwnProperty is not a functio
 
                
        
    

In that case, we can use the call method to invoke the hasOwnProperty and passing the object as its this.

        
newObj = Object.create(null)
newObj.prop1="";
 
console.log(({}).hasOwnProperty.call(newObj,"prop1"))  //true
 
                
        
    

Deleting Non Configurable Propertiess

The JavaScript does not stop anyone from overriding the hasOwnProperty property down the prototype chain.

The code below declares the hasOwnProperty and always returns false. We can use the same workaround, we used in the previous section

        
const obj = {
    prop1: '1',
    hasOwnProperty() {
        return false
    }
};
 
//uses the hasOwnProperty declared in the obj
console.log(obj.hasOwnProperty('prop1'));  // false
 
//Work around using the call method
console.log(({}).hasOwnProperty.call(obj,"prop1"))  //true
 
                
        
    

Read More

  1. JavaScript Tutorial
  2. Objects in JavaScript
  3. Create Objects in JavaScript
  4. Object Properties
  5. Computed Property Names
  6. Object Literal
  7. Constructor functions
  8. DefineProperty
  9. Property Descriptors Enumerable, Writable & Configurable
  10. Object Destructuring