Javascript Check If Key Exists

Richelle John
3 min readJun 27, 2024

--

Determining if a given key exists within an object is a typical issue for developers in the complex world of JavaScript. It’s a simple but important skill that can make a big difference in the consistency and flow of your code. We’ll discover the several techniques for carrying out this check, dissect their subtleties, and learn how to use them — even in intricately layered structures — in this all-inclusive book. You will have the knowledge and skills necessary to create reliable, error-free JavaScript apps by the time you finish reading this tutorial.

The in Operator: A Preliminary Check

Using the in operator is one of the simplest ways to determine whether a key is present in a JavaScript object. This operator returns a boolean that is true if the key is located and operates without a hitch.

const person = {
name: 'John',
surname: 'Doe',
age: 23,
email: '[email protected]'
};

console.log('name' in person); // Returns true
console.log('address' in person); // Returns false

This person object has four keys: email, age, surname, and name. We just need to put the in operator before the object and the questioned key to confirm that the name key is there. The answer comes back quickly and clearly: yes means the key is there, and false implies it is not. For fast checks, it works well and efficiently.

Harnessing hasOwnProperty

Step up and you’ll come across the hasOwnProperty function. This robust method is part of the prototype of every JavaScript object and has one particular function: it determines if the key is an owned property of the object directly or whether it is inherited through the prototype chain.

const person = {
name: 'John',
surname: 'Doe',
age: 23,
email: '[email protected]'
};

console.log(person.hasOwnProperty('name')); // Returns true
console.log(person.hasOwnProperty('address')); // Returns false

We perform a direct call on the object using hasOwnProperty, supplying the key as an argument. It determines if the key is an intrinsic characteristic of the thing, a true evidence of its existence.

A Brief Remedy: Comparing with Undefined

You could use a more straightforward technique in some situations where it’s important to be brief and compare the key’s value to undefined. This specific approach is not just condensed but also incredibly expressive.

const person = {
name: 'John',
surname: 'Doe',
age: 23,
email: '[email protected]'
};

console.log(person.name !== undefined); // Returns true
console.log(person.address !== undefined); // Returns false

Above, we have simply determined whether the name key’s value deviates from undefined. If so, the answer is definitely yes — the key is a component of the item.

You Might Also Like: What Is Mastery Learning, And How Does It Apply To Coding?

Performance

See this benchmark for a performance comparison of the methods in, hasOwnProperty and key is undefined:

Navigating Nested Objects

The problem occurs when items are nested structures rather than just flat ones. In these situations, checking keys necessitates a combination of methods using the in operator and hasOwnProperty function.

const person = {
name: 'John',
surname: 'Doe',
age: 23,
email: '[email protected]',
address: {
country: 'Poland',
street: 'Krzywa 12',
city: 'Katowice'
}
};

console.log('address' in person && 'country' in person.address); // Returns true
console.log(person.hasOwnProperty('address') && person.address.hasOwnProperty('country')); // Returns true
console.log(person.address.country !== undefined); // Returns true

First, we confirm that the address key is present in the person object inside this complex web of nested objects. After confirmation, we investigate further to see if the address object has the country key. The ultimate aim is to make sure that every link in the property chain is firmly formed, regardless of whether you pick for the optional chaining, the hasOwnProperty method, or the in operator.

EndNote

We’ve gone over the many ways to check if keys are present in both flat and nested JavaScript objects in this comprehensive article. The main conclusions are as follows:

--

--

Richelle John

With over five years' experience in leading marketing initiatives across Europe and the US, I am a digital marketing expert. Visit Here https://cutt.ly/jwrH63rV