Javascript check if object key exists – How to Check if a Key Exists in a JavaScript Object?

Checking if a Key Exists in a JavaScript Object

Javascript check if object key exists: Checking if a key exists in a JavaScript Object can be done using the following ways

Method #1: Using ‘in’ Operator

Check if key exists javascript: Use the in operator, to check if a key exists in a JavaScript object. If the key is found in the given object or its prototype chain, the in operator returns true.

For Example:

"key" in gvn_obj

Example1:

// Create an object say Employ
const Employ = {
  EmpId: 2122,
  EmpName: 'Nick'
};

// Check if EmpId is present in the Employ object using the 
// 'in' operator. If it is present it prints true else false
// Here it prints true
console.log('EmpId' in Employ); 
// salary is NOT present in the Employ object hence it prints false
console.log('salary' in Employ); 

Output:

true
false

Explanation:

Here EmpId is present in the Employ object so it prints true
salary is NOT present in the Employ object hence it prints false

When using the in operator, the syntax is:

 str in object.

The value before the in keyword should be of the ‘string‘ or ‘symbol‘ type. Any value that is not a symbol is automatically converted to a string.

Example2:

// Create an object say Employ
const Employ = {
  1: 2122,
  EmpName: 'Nick'
};

// Here 1 is given as a string and it is present in Employ object
// so it prints true
console.log('1' in Employ); 
// Here 1 automatically gets converted to string hence it prints true
console.log(1 in Employ); 

Output:

true
true

NOTE:

In javascript, object keys can only be of the string or symbol types.
Despite the fact that our object appears to have a key of type integer, it is actually a string.

In our second console.log statement, the in operator will convert the 1 to a string.

Method #2: Using hasOwnProperty Method

Check if key is in object javascript: We can use the Object.hasOwnProperty() method to check if an object has a key,

For Example:

gvn_object.hasOwnProperty('key').

If the key exists in the object, the Object.hasOwnProperty() method returns true; otherwise, it returns false.

Example:

// Create an object say Employ
const Employ = {
  EmpId: 2122,
  EmpName: 'Nick'
};

// Check if EmpId is present in the Employ object using the 
// hasOwnProperty() method.
// If it is present it prints true else false
// Here it prints true since EmpId is present in Employ object
console.log(Employ.hasOwnProperty('EmpId'));

// salary is NOT present in the Employ object hence it prints false
console.log(Employ.hasOwnProperty('salary'));

Output:

true
false

Explanation:

Here EmpId is present in the Employ object so it prints true
salary is NOT present in the Employ object hence it prints false

NOTE:

  • The object.hasOwnProperty() method differs from the in operator.
  • The in operator checks for a key in an object and its prototype chain, but the object.hasOwnProperty() method just looks for the key directly on the object.

Method #3: Using Optional chaining

Javascript check if key exists: To check if a key exists in an object, we can use the Optional chaining (.) operator, such as :

gvn_object.key

If the key is present on the object, the Optional chaining operator returns the value of the key; otherwise, it returns undefined.

Example:

// Create an object say Employ
const Employ = {
  EmpId: 2122,
  EmpName: 'Nick'
};

// Check if EmpId is present in the Employ object using  
// Optional chaining (.) operator
// If it is present it prints value of key else undefined
// Here it prints 2122 since EmpId is present in Employ object
console.log(Employ.EmpId); 
// salary is NOT present in the Employ object hence it prints undefined
console.log(Employ.salary);

Output:

2122
undefined

Explanation:

Javascript check if key exists in object: We used the Optional chaining operator in the code sample to check if the EmpId and salary keys were present in the Employ object.

Because the EmpId key exists, Employ?.EmpId evaluates to 2122 and since the salary key does not exist, the Optional chaining operator returns undefined.

NOTE:

This conditional check would fail if an object had keys with 'undefined' values;
in such case, this approach would return a false negative.

Example:

// Create an object say Employ
const Employ = {
  EmpId: undefined,
};

console.log(Employ.EmpId); // prints undefined

if (Employ.EmpId !== undefined) {
  // The `EmpId` key exists in the Employ object,
  // but this never runs
  console.log('btechgeeks')
}

Output:

undefined

Explanation:

Even though the name key exists on the object in the code example, our conditional check does not account for the situation where its value is set to undefined.