Categories
JavaScript Answers

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

Spread the love

To check if a key exists in a JavaScript object, we use the hasOwnProperty method.

For instance, we write

const obj = { key: undefined };
console.log(obj.hasOwnProperty("key"));

to call obj.hasOwnProperty to check if the 'key' property is in the obj object itself.

By John Au-Yeung

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

Leave a Reply

Your email address will not be published.