Articles by FavTutor
  • AI News
  • Data Structures
  • Web Developement
  • AI Code GeneratorNEW
  • Student Help
  • Main Website
No Result
View All Result
FavTutor
  • AI News
  • Data Structures
  • Web Developement
  • AI Code GeneratorNEW
  • Student Help
  • Main Website
No Result
View All Result
Articles by FavTutor
No Result
View All Result
Home Web Developement

JavaScript hasOwnProperty() Explained (with Examples)

Komal Bhatia by Komal Bhatia
January 17, 2024
Reading Time: 5 mins read
JavaScript hasOwnProperty() Method
Follow us on Google News   Subscribe to our newsletter

In this article, we will be taking a deep dive into the hasOwnProperty() method that helps in checking whether a property exists within an object. Let us get started!

What is the hasOwnProperty() Method in JavaScript?

We will begin with a short introduction of Objects.  Objects are non-primitive data types in Javascript that store an unordered collection of key-value pairs. The association between the key and value is called property, so objects are a collection of properties.

For example, we have defined an object “student” with various properties below:

let student={
    name:'Komal',
    rollNumber:200101,
    age:21
}

Several properties such as name, rollNumber, and age are defined for the object student”. The name, rollNumber, and age here indicate the keys, and “Komal”, 200101 and 21 are the values corresponding to the related keys.

The hasOwnProperty() is an in-built method in JavaScript is used to check if an object contains a property. This method takes an argument in the string form as the parameter and checks whether that string (the property) is present in the object or not.

It returns a boolean value. If it returns true, it means that the property exists in the object. If it returns false, it means that the object doesn’t contain that particular property.

The syntax for using this method is as follows:

object_name.hasOwnProperty(‘property_name)

Let us understand it with the help of the previous example. We have an object ‘employee’ with certain properties(key-value pairs). Now, We will check if the object employee contains a certain property or not using this method. The code for the same is given below:

//defining an object employee
let employee={
    empId:2301,
    name: "Joshua",
    email:"[email protected]",
    age:32
};

//both the statements below return true
console.log(employee.hasOwnProperty('name'));
console.log(employee.hasOwnProperty('age'));

//below statement returns false
console.log(employee.hasOwnProperty('salary'));

Output:

true
true
false

The code above uses the inbuilt method ‘hasOwnProperty(‘property_name’)’ and returns true for the properties – name and age. It returns false when checking for “salary”.

When working with objects in JavaScript, it’s important to distinguish between properties that are directly defined on the object and those inherited from its prototype. Using hasOwnProperty allows developers to ensure they are only considering properties that belong directly to the object.

The hasOwnProperty() method also considers properties with values of null or undefined as direct properties of the object. Let us see an example of the sample:

//defining an object employee
let employee={
    empId: null,
    name: undefined,
};

//both the statements below return true
console.log(employee.hasOwnProperty("empId"));
console.log(employee.hasOwnProperty("name"));

In this example, we have an object employee with properties empId and name, both having values of null and undefined, respectively. Themethod returns true for both properties, indicating that they are direct properties of the object.

hasOwnProperty() vs the “in” Operator

While the hasOwnProperty() method is effective for checking the direct properties of an object, the “in” operator is used to determine if a property exists in an object, regardless of whether it is a direct property or an inherited property.

The syntax of the in operator is:

“propertyName” in objectName;

where, 

propertyName: name of the property to check

objectName: name of the object in which property has to be checked

Let us see an example of the same:

//defining an object employee
let employee={
    empId:2301,
    name: "Joshua",
    email:"[email protected]",
    age:32
};

//both the statements below return true
'name' in employee;
'age' in employee;

//below statement returns false
'salary' in employee

Output:

true
true
false

The above code checks if the name and age properties exist in the object ‘employee’ and hence returns true, whereas returns false when checking for the property ‘salary’ as it doesn’t exist in the object.

It’s important to choose the appropriate method based on the specific use case. 

  • If we want to check for direct properties only, use the hasOwnProperty() method. 
  • If we need to check for properties in both direct and inherited ways, the “in” operator is the preferred choice.

Can hasOwnProperty be used with any type of object? Yes, this method available on all objects in JavaScript. It can be used with objects created using object literals, constructor functions, or instances of built-in classes.

However, one of the common limitations when iterating over object properties, leading to unintended inclusion of inherited properties. It’s essential to keep this in mind to ensure accurate and expected behavior when working with objects.

Conclusion 

In this article, we discussed the objects and the hasOwnProperty() method to check if a property exists within an object. Then we compared the hasOwnProperty() method and the “in” operator. Both methods are used to check the existence of a property within an object but have certain differences.

ShareTweetShareSendSend
Komal Bhatia

Komal Bhatia

I am a dedicated Computer Science student with a strong track record of academic excellence. I am a highly motivated individual with a passion for providing creative and innovative solutions to complex problems. I possess skills in the domain of C++ and web development and am always eager to contribute to the field of Software Development.

RelatedPosts

Javascript Animation Libraries

Top 10 JavaScript Animation Libraries in 2025

February 19, 2025
JavaScript Interview Questions

Top 40 JavaScript Interview Questions and Answers in 2024

April 1, 2025
Best React UI Component Libraries

10 Must-Know React UI Component Libraries for Web Devs 2024

May 7, 2024
Currying in JavaScript

Currying in JavaScript Explained (with Examples)

March 15, 2024
Javascript Format Currency

Javascript Program for Format Currency

April 8, 2025

About FavTutor

FavTutor is a trusted online tutoring service to connects students with expert tutors to provide guidance on Computer Science subjects like Java, Python, C, C++, SQL, Data Science, Statistics, etc.

Categories

  • AI News, Research & Latest Updates
  • Trending
  • Data Structures
  • Web Developement
  • Data Science

Important Subjects

  • Python Assignment Help
  • C++ Help
  • R Programming Help
  • Java Homework Help
  • Programming Help

Resources

  • About Us
  • Contact Us
  • Editorial Policy
  • Privacy Policy
  • Terms and Conditions

Website listed on Ecomswap. © Copyright 2025 All Rights Reserved.

No Result
View All Result
  • AI News
  • Data Structures
  • Web Developement
  • AI Code Generator
  • Student Help
  • Main Website

Website listed on Ecomswap. © Copyright 2025 All Rights Reserved.