javascript
  1. javascript-hasownproperty

JavaScript hasOwnProperty()

Syntax

object.hasOwnProperty(property)

The hasOwnProperty() method is a built-in method in JavaScript, which returns a boolean indicating whether the object has the specified property or not.

Example

Consider the following example:

const car = {
  make: 'Honda',
  model: 'Accord',
  year: 2022
};

console.log(car.hasOwnProperty('model')); // true
console.log(car.hasOwnProperty('color')); // false

In this example, we are creating an object car, with properties make, model, and year. We are then using the hasOwnProperty() method to check whether the car object has the properties model and color. The first call to hasOwnProperty() returns true since the car object has a model property. The second call to hasOwnProperty() returns false since the car object does not have a color property.

Output

true
false

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Car Object Example</title>
</head>
<body>

<!-- Display area for the output -->
<div id="output"></div>

<script>
    // Your JavaScript code
    const car = {
        make: 'Honda',
        model: 'Accord',
        year: 2022
    };

    // Check if the object has a property
    const hasModelProperty = car.hasOwnProperty('model');
    const hasColorProperty = car.hasOwnProperty('color');

    // Display the output in the HTML document
    document.getElementById('output').innerHTML = `
        <p>Does the car have a 'model' property? ${hasModelProperty}</p>
        <p>Does the car have a 'color' property? ${hasColorProperty}</p>
    `;
</script>

</body>
</html>
Try Playground

Explanation

The hasOwnProperty() method in JavaScript checks if the given property exists in the object or not. The method takes one argument, which is the property to be checked. If the property exists, the method returns true; otherwise, it returns false.

It is important to note that the hasOwnProperty() method only checks for properties that are defined on the object itself, not inherited properties. In other words, if a property is inherited from the object's prototype, hasOwnProperty() will return false.

Use

The hasOwnProperty() method is particularly useful when dealing with objects in JavaScript. It allows you to check whether an object has a specific property or not, without raising an error if the property does not exist.

This method is often used to avoid errors when iterating over an object's properties using a loop. In such cases, it is important to use the hasOwnProperty() method to ensure that you are only processing properties that belong to the object itself and not inherited from the prototype.

Important Points

Here are some important points to note when using the hasOwnProperty() method in JavaScript:

  • The method checks if an object has a property with the given name and returns true or false accordingly.
  • It only checks for properties that are defined on the object itself and not inherited properties.
  • The in operator can also be used to check if an object has a property, but it will return true for inherited properties as well.
  • It is important to use the hasOwnProperty() method when iterating over an object's properties to ensure that only properties belonging to the object itself are processed.

Summary

The hasOwnProperty() method in JavaScript is a useful built-in method that allows you to check whether an object has a specific property or not. Unlike other methods such as the in operator, hasOwnProperty() only checks for properties defined on the object itself and not inherited properties. This makes it particularly useful when iterating over an object's properties, ensuring that only the object's own properties are processed.

Published on: