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

Check If Object has a Property in JavaScript (with code)

Komal Bhatia by Komal Bhatia
January 17, 2024
Reading Time: 5 mins read
Check If a Property Exists in an Object in JavaScript
Follow us on Google News   Subscribe to our newsletter

Just like we use hashmaps in C++ that store data in the form of “key-value” pairs, We use Objects in JS to store data in the same way, i.e. in the form of key-value pairs. In this article, we will be taking a deep dive into what Javascript objects are and how to check if a property exists in an object. We will discuss various methods to check for key existence in an object.

3 Ways to Check if Property exists in Object

The association between key and value in an object is called property, so objects are a collection of properties. When working with objects, we might need to check if the property exists before attempting to access its value. Also, this allows you to implement conditional logic based on whether a specific property is present or not.

Let us discuss the various methods to check if the JavaScript object has a particular property:

1) Using the ‘in’ Operator

A simple way to check if the object contains a certain property is by using the “in” operator. It returns true if the property is found, and false otherwise. 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.

2) Using the hasOwnProperty() Method

The best wa to check if a JavaScript object contains a property is by using an inbuilt ‘hasOwnProperty()’ method. 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”.

3) Using the typeof Operator

The typeof operator is used to determine the type of a value in JavaScript. It can also be used to check if a property exists in an object by comparing the property to undefined. If the type of the property comes out to be undefined, then the property does not exist in the object. 

The basic syntax for using the typeof operator:

typeof(object_name.property_name)

where, 

property_name: name of the property to check

object_name: name of the object in which we check a specific property

Let us see an example:

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

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

//below statement returns false
console.log(typeof(employee.rollno));

Output:

number
string
undefined

In this article, we check the type of various object properties to see if they belong to an object or not using the typeof operator. We see that the properties “empId” and “name” belong to the object and hence they return number and string as types respectively. On the other hand, the “rollno” property doesn’t belong to the object and hence returns undefined. We have got the desired result. 

Conclusion

In this article, We discussed various methods how to check if the JavaScript object has a property. Some of these include using the “in” operator, using the hasOwnPropertyMethod(), and the typeof operator. Also, learn how to check if the key exists in a JavaScript object.

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.