Javascript Check if Object has Key


JavaScript object is a collection of properties. Each property has a key and a value, they always come in pairs.

The key can be of any type, but it is usually a string. The value can be of any type, including other objects.

Javascript object structure
JavaScript object structure

In this tutorial, we will look at 5 different ways to check if an object has a particular key.


1. Using hasOwnProperty() method

The hasOwnProperty() is an object method that returns a true or false value depending on whether the object has the specified property as its own property.

Note: It does not check if the property is present in the object's prototype chain.

The hasOwnProperty() method is called on the object and the property name is passed as an argument to the method.

For example, if we want to check if the object person has a property name, we will call the method as person.hasOwnProperty('name').

Example

let person = {
  name: 'Alice',
  age: 24
};

console.log(person.hasOwnProperty('name')); // ✅ true
console.log(person.hasOwnProperty('age')); // ✅ true
console.log(person.hasOwnProperty('address')); // ❌ false

As you can see in the above example, the hasOwnProperty() method returns true for 'name' and 'age' properties because they exist, but returns false for 'address' property as it does not exist.


2. Using in operator

The in operator is used to check whether a property exists in an object or not.

It is used as property in object. If the property exists in the object, it returns true, else it returns false.

Example

let person = {
  name: 'Alice',
  age: 24
};

console.log('name' in person); // ✅ true
console.log('age' in person); // ✅ true
console.log('address' in person); // ❌ false

Please note that the in operator can also check if a property exists in the object's prototype chain.

For example, if we have an object person and we add a property address to its prototype chain, then the in operator will return true for 'address' property.

Example

function Person(name, age) {
  this.name = name;
  this.age = age;
}
Person.prototype.address = 'New York';

let person = new Person('Alice', 24);

console.log('name' in person); // ✅ true
console.log('age' in person); // ✅ true
console.log('address' in person); // ✅ true

3. Using Object.keys() method

The Object.keys() method is another way to check if an object has a particular key.

It returns an array of all the keys present in the object. You need to pass the object as an argument to the method.

Once you have the array of keys, you can check if the array contains the key you are looking for using the includes() method.

Example

let person = {
  name: 'Alice',
  age: 24
};

console.log(Object.keys(person).includes('name')); // ✅ true
console.log(Object.keys(person).includes('age')); // ✅ true

4. Using Object.getOwnPropertyNames() method

The Object.getOwnPropertyNames() method is similar to the Object.keys() method. It also return an array of all the keys present in the object.

Only difference is that it returns all the keys, including the non-enumerable properties and the properties in the prototype chain.

So, if you want to check if a property exists in the object's prototype chain, you can use the Object.getOwnPropertyNames() method.

Same as above we can use include() method to check if the array contains the key we are looking for.

Example

function Person(name, age) {
  this.name = name;
  this.age = age;
}
Person.prototype.address = 'New York';

let person = new Person('Alice', 24);

// check if key exist
let keys = Object.getOwnPropertyNames(person);
console.log(keys.includes('name')); // ✅ true
console.log(keys.includes('age')); // ✅ true
console.log(keys.includes('address')); // ✅ true

5. Checking for undefined

Another way to check if a property exists in an object is to check if it is undefined.

You can simply access the property and check if it is undefined or not.

If the property is undefined, it means that the property does not exist in the object.

Here is an example:

Example

let person = {
  name: 'Alice',
  age: 24
};

console.log(person.name !== undefined); // ✅ true
console.log(person.age !== undefined); // ✅ true
console.log(person.address !== undefined); // ❌ false

Conclusion

Discussed above are 5 different ways to check if object has key or not.

Among these methods the in operator is the most commonly used method to check if an object has a particular key.

However, if you want to check if a property exists in the object's prototype chain, then you can use the Object.getOwnPropertyNames() method.

That's all for this tutorial. Happy Learning! 😇