Check if a Key exists in a JavaScript Object

avatar

Borislav Hadzhiev

Last updated: Jul 25, 2022

banner

Photo from Unsplash

Check if a Key exists in an Object with the in Operator #

Use the in operator to check if a key exists in an object, e.g. "key" in myObject. The in operator will return true if the key is present in the object, otherwise false is returned.

index.js
const person = { name: 'John', }; console.log('name' in person); // 👉️ true console.log('age' in person); // 👉️ false if ('name' in person) { // 👇️ this runs console.log('key is in contained in object'); }

The syntax used with the in operator is: string in object.

If you need to check if a key doesn't exist in an object, wrap the expression in parentheses.

index.js
const person = { name: 'John', }; console.log(!('age' in person)); // 👉️ true console.log(!('name' in person)); // 👉️ false if (!('age' in person)) { // 👇️ this runs console.log('key is not in object'); }

Wrapping the expression in parentheses allows us to evaluate it as a whole.

Check if a Key exists in an Object with Optional Chaining #

Use the optional chaining operator to check if a key exists in an object, e.g. - myObject?.key. If the key exists in the object, the optional chaining operator will return the key's value, otherwise it returns undefined.

index.js
const person = { name: 'John', }; console.log(person?.name); // 👉️ John console.log(person?.age); // 👉️ undefined if (person?.name !== undefined) { // the key exists on the object }

We used the optional chaining (?.) operator to check if the name and age keys exist in the object.

The name key exists in the object, so person?.name evaluates to John. The age key doesn't, so the optional chaining operator returns undefined.

This conditional check wouldn't work if you have keys with undefined values in the object.
index.js
const person = { name: undefined, }; console.log(person?.name); // 👉️ undefined if (person?.name !== undefined) { // the `name` key exists, but this never runs }

The name key exists in the object, but the condition in our if statement does not account for the case where the key is set to undefined.

Depending on your use case, you might or might not want to consider a key with an undefined value as present.

Check if a Key exists in an Object with Object.hasOwnProperty #

Use the Object.hasOwn() method to check if a key exists in an object, e.g. - Object.hasOwn(person, 'name'). The Object.hasOwn method returns true if the key exists in the object, otherwise false is returned.

index.js
const person = { name: 'John', }; console.log(Object.hasOwn(person, 'name')); // 👉️ true console.log(Object.hasOwn(person, 'age')); // 👉️ false if (Object.hasOwn(person, 'name')) { // 👇️ this runs console.log('key exists in the object'); }

The Object.hasOwn method returns true if the specified property exists in the object.

If the property is inherited, or does not exist, the method returns false.

The difference between the Object.hasOwn method and the in operator is that the in operator checks for the key in the object and its prototype chain, whereas the Object.hasOwn method only checks for the existence of the key directly on the object.

Further Reading #

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.