Verify if a JavaScript object has a certain property
This post will discuss how to verify if an object has a certain property in JavaScript.
There are several ways to check whether an object contains a specific property in JavaScript. Here are some of the possible options:
1. Using Object.prototype.hasOwnProperty() function
JavaScript already provides a built-in function Object.prototype.hasOwnProperty() for checking if an object contains the specific property or not. The hasOwnProperty() function returns true if the object has the specified property as their own property and not inherited, and false otherwise. In order words, the specified property should be attached to the object itself and not its prototypes. The following example determines whether the object obj contains the age property:
|
1 2 3 4 5 |
let obj = { name : 'John', age : 20, sex : 'male' }; let prop = 'age'; let hasKey = obj.hasOwnProperty(prop); console.log(hasKey); // true |
However, it is not completely full-safe to call the hasOwnProperty() function on the object. The above code will fail if the object has some property named hasOwnProperty. We can handle it by calling the Object.prototype.hasOwnProperty.call() function instead:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
let obj = { name : 'John', age : 20, sex : 'male', hasOwnProperty: true }; let prop = 'age'; try { let hasKey = obj !== undefined && obj.hasOwnProperty(prop); } catch (e) { // TypeError: obj.hasOwnProperty is not a function console.log(e.name + ': ' + e.message); } let hasKey = Object.prototype.hasOwnProperty.call(obj, prop); console.log(hasKey); // true |
If we have an array of objects and want to check if any of the objects have the specified property, we can use it with the some() function. This function searches for atleast one element in the array that passes the test implemented by the provided function.
|
1 2 3 4 5 |
let obj = [{ 'one' : 1 }, { 'two' : 2}, { 'three' : 3 }]; // An array of objects let prop = 'two'; let hasKey = obj.some(o => Object.prototype.hasOwnProperty.call(o, prop)); console.log(hasKey); // true |
2. Using in operator
Another option is to use the in operator, which is a built-in operator that returns true if the property exists in an object, and false otherwise. For example:
|
1 2 3 4 5 6 7 |
let obj; let prop = 'age'; // Check if the object has the age property let hasKey = obj !== undefined && prop in obj; console.log(hasKey); // true |
If we have an object array and want to check if any of the objects have the specified property, we can do something like:
|
1 2 3 4 5 |
let obj = [{ 'one' : 1 }, { 'two' : 2}, { 'three' : 3 }]; // An array of objects let prop = 'two'; let hasKey = obj.some(o => prop in o); console.log(hasKey); // true |
3. Using typeof operator
A third option is to use the typeof operator, which is a built-in operator that returns a string indicating the type of the value. We can use this operator to check if the property is not undefined, which means it exists in the object.
|
1 2 3 4 5 |
let obj = { name : 'John', age : 20, sex : 'male' }; // Check if the object has the 'age' property let hasKey = typeof obj.age !== 'undefined'; console.log(hasKey); // true |
However, it is not completely safe to call this operator on the object, as it will not work if the property has an undefined value. For example:
|
1 2 3 4 5 |
let obj = { name : 'John', age : undefined, sex : 'male' }; // Check if the object has the 'age' property let hasKey = typeof obj.age !== 'undefined'; console.log(hasKey); // false (even though the property exists) |
That’s all about verifying if a object has a certain property in JavaScript.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)