Determine if a key exists in a JavaScript object
This post will discuss how to determine whether a key exists in a JavaScript object.
The first solution that comes to mind is to use the strict equality operator to compare the given key’s value with undefined.
|
1 2 3 4 5 6 7 8 9 |
var obj = { one: 1, two: 2, three: 3 }; var key = 'two'; var hasKey = (obj[key] !== undefined); console.log(hasKey); /* Output: true */ |
This would have worked in all cases except when the key exists, but its value is actually undefined. This behavior is demonstrated below:
|
1 2 3 4 5 6 7 8 9 |
var obj = { one: 1, two: undefined, three: 3 }; var key = 'two'; var hasKey = (obj[key] !== undefined); console.log(hasKey); /* Output: false */ |
This post provides an overview of some of the available alternatives to determine whether a key exists in an object correctly.
1. Using hasOwnProperty() method
The hasOwnProperty() method returns true if an object contains the specified property, which is a direct property of that object and not an inherited one. The following example checks if the object obj has a property named two:
|
1 2 3 4 5 6 7 8 9 |
var obj = { one : 1, two : 2, three : 3 }; var key = 'two'; var hasKey = Object.prototype.hasOwnProperty.call(obj, key); console.log(hasKey); /* Output: true */ |
2. Using In operator
Another approach is to use the in operator, which returns true if the specified property is found in the specified object or its prototype chain. The following example demonstrates.
|
1 2 3 4 5 6 7 8 9 |
var obj = { one : 1, two : 2, three : 3 }; var key = 'two'; var hasKey = key in obj; console.log(hasKey); /* Output: true */ |
3. Using Reflect.has() method
The static Reflect.has() method allows you to check if a property is in an object. It works like the in operator as a function.
|
1 2 3 4 5 6 7 8 9 |
var obj = { one : 1, two : 2, three : 3 }; var key = 'two'; var hasKey = Reflect.has(obj, key); console.log(hasKey); /* Output: true */ |
4. Using Underscore/Lodash Library
If you’re already using the Underscore or Lodash library, consider using the _.has method, which returns true when the given key is a direct property of the object.
|
1 2 3 4 5 6 7 8 9 10 11 |
var _ = require('lodash'); // or, use underscore var obj = { one : 1, two : 2, three : 3 }; var key = 'two'; var hasKey = _.has(obj, key); console.log(hasKey); /* Output: true */ |
5. Using Object.keys() function
Finally, you can iterate over all properties of the object and check each key one by one. This can be easily done using the Object.keys() and Array.prototype.some() method, as shown below:
|
1 2 3 4 5 6 7 8 9 |
var obj = { one : 1, two : 2, three : 3 }; var key = 'two'; var hasKey = Object.keys(obj).some(x => x == key); console.log(hasKey); /* Output: true */ |
That’s all about checking if a key exists in a JavaScript object.
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 :)