Horje
JavaScript Object hasOwn() Method

JavaScript hasOwn() method is used to check if the object has the specified property or not. It returns true if the property exists else false. This method was introduced as a replacement for the Object.hasOwnProperty() method. It is different from in operator as it does not check for inherited properties.

Syntax:

Object.hasOwn(obj, prop)

Parameters:

This method takes two parameters

  • obj: This is the JavaScript object on which the check is to be applied
  • prop: It is the property on which the check is to be applied

Return Value:

  • A boolean value true if exists else false.

Example 1: This example uses the hasOwn() method to check if a property exists or not

JavaScript
let details = {
    name: "Raj",
    course: "DSA",
    website: "horje.org",
}

console.log(Object.hasOwn(details, 'name'));
console.log(Object.hasOwn(details, 'course'));
console.log(Object.hasOwn(details, 'phone number'));

Output:

true
true
false

Example 2: This example will compare the in operator and Object.hasOwn() method.

JavaScript
let details = {
    name: "Raj",
    course: "DSA",
    website: "horje.org",
}

console.log(Object.hasOwn(details, 'name'));
console.log('name' in details);

console.log(Object.hasOwn(details, 'toString'));
console.log('hasOwnProperty' in details);

Output: The in operator returns true for even inherited properties like toString is an inherited method for all JavaScript objeccts.

true
true
false
true

Example 3: This method compares the hasOwnProperty() method and hasOwn() method.

JavaScript
let details = Object.create(null);
details.course = "DSA";

console.log(Object.hasOwn(details, "course"));
console.log(Object.hasOwnProperty(details, "course"))

Output: The hasOwnProperty() method does not work on null objects but hasOwn() method works. So it is better than hasOwnProperty() method

Supported Browsers:

  • Chrome
  • Edge
  • Firefox
  • Opera
  • Safari

JavaScript Object hasOwn() Method- FAQs

How do you use the Object.hasOwn() method?

To use Object.hasOwn(), you pass the object and the property name as arguments. The method returns true if the object has the specified property as its own property, and false otherwise.

How does Object.hasOwn() handle non-object arguments?

If the first argument is not an object, Object.hasOwn() will coerce it to an object. However, if the argument is null or undefined, it will throw a TypeError.

How does Object.hasOwn() handle properties with undefined values?

Object.hasOwn() returns true if the property exists as an own property, even if its value is undefined.

How does Object.hasOwn() interact with frozen objects?

Object.hasOwn() can be used on frozen objects to check for the existence of own properties. Freezing an object does not affect the hasOwn() method.

What is the most common use of the Object.hasOwn() method?

  • Checking Object’s Own Properties: Determining if an object has a specific property as its own property, avoiding properties inherited from the prototype chain.
  • Ensuring Property Existence: Validating that an object has certain properties before performing operations on them.
  • Avoiding Prototype Pollution: Preventing issues related to prototype pollution by ensuring that properties are direct properties of the object.

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




Reffered: https://www.geeksforgeeks.org


JavaScript

Related
Concept of JavaScript Closures Inside Loops Concept of JavaScript Closures Inside Loops
Why don't self-closing script elements work ? Why don't self-closing script elements work ?
JavaScript Intl PluralRules() Constructor JavaScript Intl PluralRules() Constructor
JavaScript Intl RelativeTimeFormat() Constructor JavaScript Intl RelativeTimeFormat() Constructor
JavaScript Intl Segementer() Constructor JavaScript Intl Segementer() Constructor

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
15