A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/javascript/javascript-object-hasownproperty-method/ below:

JavaScript Object hasOwnProperty() Method - GeeksforGeeks

JavaScript Object hasOwnProperty() Method

Last Updated : 12 Jul, 2025

The hasOwnProperty() method in JavaScript checks if an object has a specific property as its own (not inherited). It returns true if the property exists directly on the object, otherwise false, making it useful for distinguishing own properties from inherited ones.

Syntax
object.hasOwnProperty( prop );
Parameters: Return Value:

It returns a Boolean value indicating whether the object has the given property as its own property.

Example 1: In this example, the hasOwnProperty() method checks if the exampleObj contains the properties "height" (which exists) and "breadth" (which doesn't exist). It returns true for "height" and false for "breadth".

JavaScript
let exampleObj = {};
exampleObj.height = 100;
exampleObj.width = 100;

// Checking for existing property
let result1 = exampleObj.hasOwnProperty("height");

// Checking for non-existing property
let result2 = exampleObj.hasOwnProperty("breadth");

console.log(result1);  // true
console.log(result2);  // false

Example 2: In this example The checkProperty() function creates a Car object and checks if the model property exists (true) and if the wheels property doesn't exist (false) using the hasOwnProperty() method.

JavaScript
function checkProperty() {

    function Car(a, b) {
        this.model = a;
        this.name = b;
    }

    let car1 = new Car("Mazda", "Laputa");

    // Checking for existing property
    result1 = car1.hasOwnProperty("model");

    // Checking for non-existing property
    result2 = car1.hasOwnProperty("wheels");

    console.log(result1);

    console.log(result2);
}
checkProperty()

We have a complete list of Object methods, and properties to check those please go through this JavaScript Object Complete Reference article.

Supported Browsers:

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4