How do I check if a particular key exists in a JavaScript object or array?
If a key doesn't exist, and I try to access it, will it return false? Or throw an error?
How do I check if a particular key exists in a JavaScript object or array? If a key doesn't exist, and I try to access it, will it return false? Or throw an error? |
|||||||||
|
Checking for undefined-ness is not an accurate way of testing whether a key exists. What if the key exists but the value is actually
You should instead use the
If you want to check if a key doesn't exist, remember to use parenthesis:
Or, if you want to particularly test for properties of the object instance (and not inherited properties), use
EDIT: For performance comparison between the methods that are |
|||||||||||||||||||||
|
quick answer
Accessing directly a missing property using (associative) array style or object style will return an undefined constant. The slow and reliable in operator and hasOwnProperty methodAs people have already mentioned here, you could have an object with a property associated with an "undefined" constant.
In that case, you will have to use hasOwnProperty or in operator to know if the key is really there. But, but at what price? so, I tell you... in operator and hasOwnProperty are "methods" that use Property Descriptor mechanism in Javascript (similar to Java reflection in the Java language). http://www.ecma-international.org/ecma-262/5.1/#sec-8.10
On the other hand, calling an object method or key will use Javascript [[Get]] mechanism. That is far way faster! benchmarkhttp://jsperf.com/checking-if-a-key-exists-in-a-javascript-array
The result was
Using hasOwnProperty
The result was
Accessing elements directly (brackets style)
The result was
Accessing elements directly (object style)
The result was
EDIT: What is the reason to assign to a property the
|
|
Are all of these methods acceptable in all commonly used browsers, e.g. IE8+?
– Justin
Nov 19 '14 at 18:03
|
||
|
@Justin Yes. It should work. You can test directly on jsperf.com/checking-if-a-key-exists-in-a-javascript-array.
– rdllopes
Nov 19 '14 at 18:33
|
||
|
+1 for benchmarking. Thank you, this is exactly the information I was hoping to find. Definitely a strong argument to write code that never assigns or expects a key to contain the value undefined.
– T.J. Compton
Mar 9 '15 at 16:18
|
||
|
I was curious how Underscore.js's has() compared, so I added it to the jsperf (version 11). Turns out it's in the slow group along with in and hasOwnProperty().
– mpoisot
Mar 10 '15 at 16:06
|
||
|
One reason I would set undefined to a hash value is that I actually wanted to delete that property key from the hash, but
delete hash[key] is much slower than hash[key] = undefined . Of course in this case it makes no sense for me to need the in operator, but it acts as a counterexample to “we should always avoid setting value to undefined”.
– Alan Tam
Aug 26 '16 at 11:18
|
It will return
This is probably the best way to check for missing keys. However, as is pointed out in a comment below, it's theoretically possible that you'd want to have the actual value be
|
|||||||||||||||||||||
|
Is likely testing only object attribute values that are very different from array keys |
||||
|
The accepted answer refers to Object. Beware using the
To test existing elements in an Array: Best way to find if an item is in a JavaScript array? |
||||
|
Three ways to check if a property is present in a javascript object:
Reference: |
||||
|
If you are using underscore.js library then object/array operations become simple. In your case _.has method can be used. Example:
returns true But,
returns false |
||||
|
Here's a helper function I find quite usefulThis Just pass it the key you want to find, and search obj (the object or array) you want to find it in.
How to use it:Searching for keys in Arrays
Searching for keys in Objects
It's been pretty reliable and works well cross-browser. |
|||||
|
Answer:
Explanation: The For that reason, it is much better practice to first use the |
||||
|
We can use -
|
vanila js
If you want to check if the object has at least one property in es2015
|
|||
|
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?