Check If A Key Exists In An Object In JavaScript

You can check if a key exists in an object in JavaScript using any of the following methods –

  1. Using the ‘key’ in ‘object’ syntax
  2. Using the hasOwnProperty() method
  3. Using the lodash library
  4. Using optional chaining (?.)

Let’s have a look at each of these methods in detail –

Check If A Key Exists In An Object In JavaScript

check if a key exists in an object in javascript

1) Using The ‘key’ in ‘object’ Syntax

This is the most common and straightforward way to check if a key exists in an object. All you need to do is use the key followed by the in keyword and then the name of the object. This will return true if the key exists and false if it doesn’t.

For example –

var obj = {

"name": "John",

"age": 30,

"city": "New York"

}

console.log("name" in obj) //true

console.log("gender" in obj) //false

You can use a conditional statement to execute some code if the key exists or doesn’t exist.

For example –

var obj = {

"name": "John",

"age": 30,

"city": "New York"

}

if ("name" in obj){

console.log("The key ‘name’ exists in the object")

}else{

console.log("The key ‘name’ does not exist in the object")

}

Output:

The key ‘name’ exists in the object

2) Using The hasOwnProperty() Method

This is another simple and easy to use method. The hasOwnProperty() method is a built-in method in JavaScript that returns true if the key exists and false if it doesn’t.

For example –

var obj = {

"name": "John",

"age": 30,

"city": "New York"

}

console.log(obj.hasOwnProperty("name")) //true

console.log(obj.hasOwnProperty("gender")) //false

You can use a conditional statement to execute some code if the key exists or doesn’t exist.

For example –

var obj = {

"name": "John",

"age": 30,

"city": "New York"

}

if (obj.hasOwnProperty("name")){

  console.log("The key ‘name’ exists in the object")

}else{

  console.log("The key ‘name’ does not exist in the object")

}

Output:

The key ‘name’ exists in the object

The difference between hasOwnProperty() and the ‘key’ in ‘object’ method is that the hasOwnProperty() method only checks for own properties and not inherited properties. So, if the key is present in the prototype chain, it will not be detected by this method.

3) Using The lodash Library

If you are using the lodash library, then you can use the _.has() method to check if a key exists in an object. The syntax for this method is –

_.has(object, key)

This method returns true if the key exists and false if it doesn’t.

For example –

var obj = {

"name": "John",

"age": 30,

"city": "New York"

}

console.log(_.has(obj, "name")) //true

console.log(_.has(obj, "gender")) //false

You can use a conditional statement to execute some code if the key exists or doesn’t exist.

For example –

var obj = {

"name": "John",

"age": 30,

"city": "New York"

}

if (_.has(obj, "name")){

console.log("The key ‘name’ exists in the object")

}else{

console.log("The key ‘name’ does not exist in the object")

}

Output:

The key ‘name’ exists in the object

4) Using Optional Chaining (?.)

This is a new feature introduced in ES2020 (ECMAScript 2020) and it lets you check if a key exists in an object without having to do any type of null or undefined checks.

The syntax for this method is –

object?.[key]

This will return true if the key exists and false if it doesn’t.

For example –

var obj = {

"name": "John",

"age": 30,

"city": "New York"

}

console.log(obj?.["name"]) //"John"

console.log(obj?.["gender"]) //undefined

You can use a conditional statement to execute some code if the key exists or doesn’t exist.

For example –

var obj = {

"name": "John",

"age": 30,

"city": "New York"

}

if (obj?.["name"] !== undefined){

  console.log("The key ‘name’ exists in the object")

}else{

  console.log("The key ‘name’ does not exist in the object")

}

Output:

The key ‘name’ exists in the object

Note that if the object has a value equal to undefined, the above code will fail.

For example,

var obj = {

"name": undefined,

"age": 30,

"city": "New York"

}

if(obj?.["name"] !== undefined){

  console.log("The key ‘name’ exists in the object")

}else{

  console.log("The key ‘name’ does not exist in the object")

}

This will output –

The key ‘name’ does not exist in the object

which is wrong as the key does exist, but with a value of undefined.

Conclusion

In this article, you learned 4 different ways to check if a key exists in an object in JavaScript. These methods are –

1) Using The in Operator

2) Using The hasOwnProperty() Method

3) Using The lodash Library

4) Using Optional Chaining (?.)

I hope this article was helpful and that you now know how to check if a key exists in an object in JavaScript.

If you have any questions or comments, please feel free to leave them down below!

Leave a Reply