Categories
JavaScript Answers

How to check if a key exists in a JavaScript object?

Spread the love

Sometimes, we want to check if a key exists in a JavaScript object.

In this article, we’ll look at how to check if a key exists in a JavaScript object.

How to check if a key exists in a JavaScript object?

To check if a key exists in a JavaScript object, we can use the in operator.

For instance, we write

const testArray = "key1" in obj;

to check if property with name 'key1‘ is in the obj object.

Conclusion

To check if a key exists in a JavaScript object, we can use the in operator.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *