Skip to content

JavaScript hasOwnProperty | Method

  • by

Use JavaScript Object hasOwnProperty() method to check if the object has the given property as its own property. This returns true if the specified property is a direct property of the object — even if the value is null or undefined.

And it returns false if the property is inherited or has not been declared at all.


Note:Object.hasOwn() is recommended over hasOwnProperty(), in browsers where it is supported.

JavaScript hasOwnProperty method

Simple example code.

  <!DOCTYPE html>

     var x = {
      'key': 1

    if ( x.hasOwnProperty('key') ) {
      console.log('has key property');




JavaScript hasOwnProperty Method

More Examples

var x = {
    y: 10
console.log(x.hasOwnProperty("y")); //true
console.log(x.hasOwnProperty("z")); //false
const object1 = {};
object1.property1 = 42;

// expected output: true

Do comment if you have any doubts or suggestions on this JS method code.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Your email address will not be published. Required fields are marked *