When we need to check if an object has a property in JavaScript, we have several methods at our disposal. This includes JavaScript operators, specific static methods from the object class, object instance method, array instance method and a custom JavaScript function.
JavaScript: Check if an Object Has a Property Methods
In
operatorObject.prototype.hasOwnProperty()
methodObject.hasOwn()
method- Check for undefined value
Object.keys()
andArray.prototype.some
method- Custom JavaScript
util
function.
Let’s take a look at each one with examples below.
6 Ways to Check If an Object Has a Property in JavaScript
1. In operator
The in operator returns true if the specified property is present inside the object or inside its prototype chain. The operator also works for the objects created with Object.create() static method.
const person = {
name: 'John',
surname: 'Doe',
age: 41
};
const hasLocation = 'location' in person;
if (hasLocation) {
console.log("We have the location data");
} else {
console.log("We don't have the location data")
}
// result
// We don't have the location data
const user = Object.create({ name: 'Donald' });
console.log('name' in user); // true
2. Object.prototype.hasOwnProperty() method
We can also use the object instance method hasOwnProperty()
to check if an object contains a specific property.
Although Object.prototype.hasOwnProperty()
has been in JavaScript for quite a time, it has some drawbacks. hasOwnProperty()
doesn’t work for objects created using Object.create(null)
because it doesn’t inherit from Object.prototype
, which makes the hasOwnProperty()
unreachable for the created object. Using hasOwnProperty()
, the object will throw an exception.
const person = {
name: 'John',
surname: 'Doe',
age: 41
};
const hasName = person.hasOwnProperty('name');
if (hasName) {
console.log(`We have name property, value ${person.name}`);
} else {
console.log("We don't have name property");
}
// result
// We have name property, value John
const user = Object.create({ name: 'Paul' });
console.log(user.hasOwnProperty('name')); // false
3. Object.hasOwn() method
As part of the ECMAScript 2022 version, we can use a static hasOwn()
method inside the Object class. Object.hasOwn()
is recommended over hasOwnProperty()
in browsers where it is supported. Object.hasOwn()
is the intended alternative for the Object.prototype.hasOwnProperty()
method.
const person = {
name: 'John',
surname: 'Doe',
age: 41
};
const hasAge = Object.hasOwn(person, 'age');
if (hasAge) {
console.log(`We have age property, value ${person.age}`);
} else {
console.log("We don't have age property");
}
// result
// We have age property, value 41
const user = Object.create({ name: 'Jorge' });
console.log(Object.hasOwn(person, 'name')); // true
4. Check for Undefined Value
When we try to access the non-existent property of an object, then we have undefined value as the result. So, we can use this approach and do something only when property value is not undefined.
const person = {
name: 'John',
surname: 'Doe',
age: 41
};
if (person.location !== undefined) {
// do some operation
} else {
console.log('Location property is not present on person object');
}
5. Object.keys() and Array.prototype.some() methods
This approach involves using Object.keys()
and Array.prototype.some()
methods.
We’re converting the object to the array of properties, and then we have a method with a predicate function where we are checking the presence of the target property name. This method has the same drawback as the Object.prototype.hasOwnProperty()
because we can’t find the object property if the object is created with Object.create()
method.
const person = {
name: 'John',
surname: 'Doe',
age: 41
};
const hasSurname = Object.keys(person).some(key => key === 'surname');
console.log(hasSurname); // true
const user = Object.create({ name: 'Thomas' });
const hasName = Object.keys(user).some(property => property === 'name');
console.log(hasName); // false
6. Custom JavaScript util function
This hasKey()
function accepts the object and target property name arguments. If both arguments are defined, then we have a for-in loop through the object, and inside each iteration, we have a check if the current property key is equal to the target one (input parameter).
const person = {
name: 'John',
surname: 'Doe',
age: 41
};
const user = Object.create({ name: 'Kevin' });
function hasKey(object, target) {
if (object && target) {
for (const key in object) {
if (key === target) {
return true;
}
}
return false;
} else {
return false;
}
}
console.log(hasKey(person, 'name')); // true
console.log(hasKey(person, 'location')); // false
console.log(hasKey(user, 'name')); // true
Best Method to Check If a Property Exists in JavaScript
In general, being proficient with JavaScript objects is essential in web development.
As you can see, we have a lot of options we can use to check the presence of the property inside the specific JavaScript object. From my experience, the “in” operator and Object.hasOwn()
method are the best ones to use in everyday work. Also, if you don’t have ECMAScript 2022 version, you can also use the “in” operator or check for undefined value.