How to check if a Key exists in a JavaScript Object

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

Implement the in operator to check if a key exists in an object, e.g. "key" in theObject. The in operator will return true if the object or its prototype chain contains the specified property.

const person = {
  name: 'Matt',
};

console.log('name' in person); 
console.log('age' in person);

if ('name' in person) {
  console.log('key is found in the object');
}
The in operator can be used to check if a key exists in an object
The in operator can be used to check if a key exists in an object.

To check if a key does not exist in an object, use the ! operator in front of the key inside parentheses.

const person = {
  name: 'Matt',
};

console.log(!('age' in person)); 

console.log(!('name' in person));

if (!('age' in person)) {
  console.log('key is not found in object');
}
check if a key doesn't exist in an object
Check if a key doesn’t exist in an object.

Check if a Key exists in an Object with Optional Chaining

The optional chaining operator can be used to check if a key exists in an object. e.g.- theObject?.key If the key does not exist, the operator will return undefined.
Example –

const person = {
  name: 'Matt',
};

console.log(person?.name); 
console.log(person?.age); 

if (person?.name !== undefined) {
  // your logic here
}
Check if a Key exists in an Object with Optional Chaining.
Check if a Key exists in an Object with Optional Chaining.

The optional chaining (?.) operator can be used to access an object’s property or to call a function. If the object is undefined or null, the operator will return undefined instead of throwing an error.

The name key exists in the object, so name person?.name to Matt. The age key doesn’t exist, so the optional chaining operator returns undefined.

Note – If an object has keys with undefined values, this conditional check will not work.

Let’s see an example.


const person = {
  name: undefined,
};

console.log(person?.name); 

if (person?.name !== undefined) {
  // the `name` key exists but this scope will not work
  console.log(person?.name)
}
A conditional check would not work if the keys in the object have undefined values.
A conditional check would not work if the keys in the object have undefined values.

The key “name” exists in the object, but our if statement doesn’t take into account the possibility that the keyis set to undefined.

Check if a Key exists in an Object with Object.hasOwn()

Use Object.hasOwn() to check if a key exists in an object. The Object.hasOwn() method returns true if the key exists in the object and was not inherited from the object’s prototype, otherwise false is returned

Example –

const person = {
  name: "Matt",
};

console.log(Object.hasOwn(person, 'name')); 
console.log(Object.hasOwn(person, 'age'));

if (Object.hasOwn(person, 'name')) {
  // This scope will run 
  console.log('key found in the object');
}
Check if a Key exists in an Object with Object.hasOwn().
Check if a Key exists in an Object with Object.hasOwn().

The Object.hasOwn() method returns a boolean value specifying if the given property exists in the object or not.

The Object.hasOwn() method checks if a key exists on an object, while the in operator checks if a key exists on an object and its prototype chain.

Summary

There are three ways to check if a key exists in a JavaScript object:

  1. using the in operator
  2. using optional chaining (?.) operator
  3. using Object.hasOwn() method