JavaScript: Object.prototype.hasOwnProperty

By Xah Lee. Date: . Last updated: .
Return true if object obj has own property key key (string or symbol). Else, false.
// Example of using Object.prototype.hasOwnProperty
const obj = {"p":1};
console.log( obj.hasOwnProperty ( "p") ); // true
console.log( obj.hasOwnProperty ( "yyy") ); // false

Example with a symbol key. [see Symbol Tutorial]

// check if a object has own property of a symbol key
const sy = Symbol();
const ob = {};
ob[sy] = 3;
console.log( ob.hasOwnProperty ( sy ) ); // true

Inherited property doesn't count.

// Object.prototype.hasOwnProperty return false if the property is not own property

const o1 = {"p":1};

// create a object o2, with parent o1
const o2 = Object.create(o1);

console.log( o2.hasOwnProperty ( "p") ); // false

console.log( "p" in o1 ); // true
// inherited

[see Access Property]

JS in Depth

JS in Depth

JS Obj Ref


JS Obj Ref

Object Object