| by Arround The Web | No comments

What is the Property in hasOwnProperty() in JavaScript

While developing/programming in JavaScript, there can be a requirement to verify if a particular entity is contained in an array or an object. For instance, integrating various functionalities based on identical features or locating the entries. In such situations, the “hasOwnProperty()” method in JavaScript assists in sorting things out, thereby providing ease at the developer’s end.

In this write-up, we will discuss the use of property in the hasOwnProperty() method in JS.

What is hasOwnProperty() in JS?

The “hasOwnProperty()” method gives the result as a boolean value. It generates “true” if the particular property exists in the object, even if the value of the specified property is undefined or null. Otherwise, it returns “false”.

Syntax

object.hasOwnProperty(prop)

In the given syntax, “prop” corresponds to the property’s name that is added to test it as a string or symbol.

Example 1: Finding the Property Using the Array’s Keys in hasOwnProperty() Method

In this example, the hasOwnProperty() method can be applied to search for a particular key in an and returns “true” if the key exists in the array. Otherwise, it returns “false”:

<script>

const myArray = {

a: 10,

b: 20

};

let c = 30;

let x = myArray.hasOwnProperty("a")

let y = myArray.hasOwnProperty("c")

console.log(x);

console.log(y);

</script>

In the above code block:

  • Firstly, create an array named “myArray” having the stated keys and values.
  • In the next step, create a variable named “c” outside the scope of an array and assign it a value.
  • Now, apply the “hasOwnProperty()” method by specifying the key contained in an array.
  • Likewise, apply a check upon the variable outside the array’s scope.
  • As a result, the former applied method will return “true” as the key is contained in an array.
  • On the other hand, it is not the case in the latter approach, so it will return “false”.

Output

In the above output, it can be observed that the corresponding outcome based on the added condition has been displayed.

Example 2: Finding the Property Using the Array’s Index in hasOwnProperty() Method

In this particular example, the “hasOwnProperty()” method will return the corresponding outcome based on the index number in an array:

<script>

const myArray = ['mango', 'apple', 'pineapple', 'strawberry'];

let x = myArray.hasOwnProperty('3')

let y = myArray.hasOwnProperty('7')

console.log(x)

console.log(y);

</script>

In the above code snippet:

  • Similarly, create an array named “myArray” having some values.
  • Now, apply the “hasOwnProperty()” method upon the declared array by specifying the stated indexes.
  • If the particular index is contained in an array, the boolean value “true” will be returned.
  • Otherwise, “false” will be displayed on the console.

Output

In the above output, it can be seen that the corresponding boolean value is displayed according to the specified indexes.

Conclusion

The “hasOwnProperty()” method in JavaScript returns the result as a boolean value if the particular property is the direct property of the object or not. In the former approach, this method displays results in the boolean form by checking the array’s element by its keys. In the latter approach, this method checks the array’s index. This article discussed the usage of property in the hasOwnProperty() using JavaScript.

Share Button

Source: linuxhint.com

Leave a Reply