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?
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 method
As 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!
benchmark
http://jsperf.com/checking-if-a-key-exists-in-a-javascript-array
.
Using in operatorThe result was
Using hasOwnPropertyThe 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
undefined
value?That question puzzles me. In Javascript, there are at least two references for absent objects to avoid problems like this:
null
andundefined
.null
is the primitive value that represents the intentional absence of any object value, or in short terms, the confirmed lack of value. On the other hand,undefined
is unknown value (not defined). If there is a property that will be used later with a proper value consider usenull
reference instead ofundefined
because in the initial moment the property is confirmed to lack a value.Compare:
Advise
Avoid objects with
undefined
values. Check directly whenever possible and usenull
to initialize property values. Otherwise, use the slowin
operator orhasOwnProperty()
method.EDIT: 12/04/2018 - NOT RELEVANT ANYMORE
As people have commented, modern versions of the Javascript engines (with firefox exception) has changed the approach for access properties. Current implementation is slower than the previous one for this particular case but difference between access key and object are neglectable.
It will return
undefined
.undefined
is a special constant value. So you can say, e.g.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
undefined
. I've never needed to do this and can't think of a reason offhand why I'd ever want to, but just for the sake of completeness, you can use thein
operatorWhile this necessarily check if a key exists, it does check for the truthiness of a value. Which
undefined
andnull
fall under.Boolean(obj.foo)
This solution works best for me because I use typescript, and using strings like so
'foo' in obj
orobj.hasOwnProperty('foo')
to check whether a key exists or not does not provide me with intellisense.Is likely testing only object attribute values that are very different from array keys
Answer:
Explanation:
The
in
operator will check if the key exists in the object. If you checked if the value was undefined:if (myObj["key"] === 'undefined')
, you could run into problems because a key could possibly exist in your object with theundefined
value.For that reason, it is much better practice to first use the
in
operator and then compare the value that is inside the key once you already know it exists.ES6 solution
using
Array#some
andObject.keys
. It will return true if given key exists in the object or false if it doesn't.One-line example.