Anjan Dutta

Javascript check if key exists in object

Javascript check if key exists in object

Updated On: 09/01/2024

In JavaScript, you can check if a key exists in an object using various methods. Here are a few approaches:

Using the hasOwnProperty method:

The hasOwnProperty method is a built-in method in JavaScript that returns a boolean indicating whether the object has the specified property as its own property.

const myObject = {
key1: 'value1',
key2: 'value2'
};
if (myObject.hasOwnProperty('key1')) {
console.log('Key "key1" exists in the object.');
} else {
console.log('Key "key1" does not exist in the object.');
}
Run Code

Syntax help:

This JavaScript code defines an object named myObject with two key-value pairs:

const myObject = {
key1: 'value1',
key2: 'value2'
};

The object myObject has two keys, 'key1' and 'key2', with corresponding string values 'value1' and 'value2'.

Following that, the code checks whether the key 'key1' exists in the myObject using the hasOwnProperty method:

if (myObject.hasOwnProperty('key1')) {
console.log('Key "key1" exists in the object.');
} else {
console.log('Key "key1" does not exist in the object.');
}

The hasOwnProperty method is a built-in method in JavaScript that returns a boolean value. If the specified key ('key1' in this case) exists in the object, it prints a message to the console saying that the key exists. Otherwise, it prints a message saying that the key does not exist.

In this specific case, since 'key1' is present in the myObject, the code would output:

Key "key1" exists in the object.

Using the in operator:

The in operator checks if a specified property is in the object or its prototype chain.

const myObject = {
key1: 'value1',
key2: 'value2'
};
if ('key1' in myObject) {
console.log('Key "key1" exists in the object.');
} else {
console.log('Key "key1" does not exist in the object.');
}
Run Code

Syntax help:

This JavaScript code also works with an object named myObject, which has two key-value pairs:

const myObject = {
key1: 'value1',
key2: 'value2'
};

Similar to the previous example, the code checks if the key 'key1' exists in the myObject, but instead of using the hasOwnProperty method, it utilizes the in operator:

if ('key1' in myObject) {
console.log('Key "key1" exists in the object.');
} else {
console.log('Key "key1" does not exist in the object.');
}

The in operator checks whether the specified property ('key1' in this case) is present in the object or its prototype chain. If the key exists, it prints a message to the console stating that the key is present; otherwise, it prints a message saying that the key does not exist.

In this particular case, since 'key1' is part of the myObject, the code would output:

Key "key1" exists in the object.

Using optional chaining (for modern environments):

If you are working in an environment that supports optional chaining (ECMAScript 2020 and later), you can use it to safely check for nested properties.

const myObject = {
nested: {
key1: 'value1'
}
};
if (myObject?.nested?.key1 !== undefined) {
console.log('Key "nested.key1" exists in the object.');
} else {
console.log('Key "nested.key1" does not exist in the object.');
}

Syntax help:

This JavaScript code works with an object named myObject, which has a nested structure:

const myObject = {
nested: {
key1: 'value1'
}
};

The code checks if the nested key 'nested.key1' exists in myObject using optional chaining (?.) and comparing it to undefined:

if (myObject?.nested?.key1 !== undefined) {
console.log('Key "nested.key1" exists in the object.');
} else {
console.log('Key "nested.key1" does not exist in the object.');
}

The optional chaining (?.) is used to safely access nested properties. If any part of the chain is null or undefined, the expression short-circuits, and the result is undefined.

The condition checks whether the nested key 'nested.key1' is not equal to undefined. If it's not undefined, it prints a message to the console saying that the key exists; otherwise, it prints a message saying that the key does not exist.

In this specific case, since 'nested.key1' is present in myObject, the code would output:

Key "nested.key1" exists in the object.

In conclusion, choose the method that best fits your specific use case. Happy Coding!