Table of contents
1.
Introduction
2.
1. Using in Operator
2.1.
Syntax
2.2.
Example
3.
2. Using hasOwnProperty() Method
3.1.
Syntax
3.2.
Example
4.
3. Using Object.keys() Method
4.1.
Syntax
4.2.
Example
5.
Frequently Asked Questions
5.1.
What is the best method to check if a key exists in an object?
5.2.
Can an object have a key with an undefined value?
5.3.
How do I check for a nested key in a JavaScript object?
6.
Conclusion
Last Updated: Feb 9, 2025
Easy

How to Check a Key Exists in JavaScript Object?

Author Sinki Kumari
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Checking if a key exists in a JavaScript object is important when working with data. It helps developers avoid errors and handle objects properly. There are different ways to check for a key, such as using the in operator, the hasOwnProperty() method, or the Object.hasOwn() method. Each method works in a different way. Some check only the object’s own properties, while others also check inherited properties. Knowing which method to use can make your code more accurate and efficient.

How to Check a Key Exists in JavaScript Object?

In this article, you will learn simple ways to check if a key exists in a JavaScript object and when to use each method.

1. Using in Operator

The in operator is a simple way to check if a key exists in an object. It returns true if the key is present in the object, even if the value associated with it is undefined.

Syntax

"key" in object

Example

const student = {
    name: "John",
    age: 21,
    course: "Computer Science"
};

console.log("name" in student);
console.log("grade" in student);
You can also try this code with Online Javascript Compiler
Run Code

 

Output

true
false


Explanation:

  • The first check returns true because the key name exists in the student object.
     
  • The second check returns false since grade is not a key in the object.

2. Using hasOwnProperty() Method

The hasOwnProperty() method is another way to verify if a key exists. It checks if the key is directly in the object, excluding any inherited properties.

Syntax

object.hasOwnProperty("key")

Example

const person = {
    firstName: "Alice",
    lastName: "Doe",
    age: 25
};

console.log(person.hasOwnProperty("firstName")); 
console.log(person.hasOwnProperty("gender"));   
You can also try this code with Online Javascript Compiler
Run Code


Output

true
false

 

Explanation:

  • The method returns true for firstName as it is a property of person.
     
  • It returns false for gender because the key is not present.

3. Using Object.keys() Method

The Object.keys() method returns an array of a given object's own property names. You can use the includes() method to check if a key exists in this array.

Syntax

Object.keys(object).includes("key")

Example

const car = {
    brand: "Toyota",
    model: "Corolla",
    year: 2022
};
console.log(Object.keys(car).includes("model"));  
console.log(Object.keys(car).includes("color")); 
You can also try this code with Online Javascript Compiler
Run Code

 

Output

true
false

 

Explanation:

  • Object.keys(car) returns ["brand", "model", "year"].
     
  • includes("model") returns true, indicating that model exists.
     
  • includes("color") returns false as color is not a key.

Frequently Asked Questions

What is the best method to check if a key exists in an object?

The hasOwnProperty() method is the best choice when you want to check only the object's own properties, while the in operator includes inherited properties.

Can an object have a key with an undefined value?

Yes, an object can have a key with an undefined value. The in operator will still return true for such keys.

How do I check for a nested key in a JavaScript object?

You can use optional chaining like object?.key?.nestedKey to safely check for nested keys without errors.

Conclusion

In this article, we discussed different ways to check if a key exists in a JavaScript object. We discussed methods like the in operator, hasOwnProperty(), and Object.hasOwn(), each with its specific use case. Understanding these techniques helps developers write more reliable and efficient code when working with JavaScript objects.

Live masterclass