How do I check if an object has a property in JavaScript?
Consider:
x = {'key': 1};
if ( x.hasOwnProperty('key') ) {
//Do this
}
Is that the best way to do it?
How do I check if an object has a property in JavaScript?
Consider:
x = {'key': 1};
if ( x.hasOwnProperty('key') ) {
//Do this
}
Is that the best way to do it?
There is method "hasOwnProperty" exists on object, but its not recommended to call this method directly because might be sometimes the object is null or some property exist on object like:
{ hasOwnProperty: false }
So better way would be:
Note: the following is nowadays largely obsolete thanks to strict mode, and
hasOwnProperty
. The correct solution is to use strict mode and to check for the presence of a property usingobj.hasOwnProperty
. This answer predates both these things, at least as widely implemented (yes, it is that old). Take the following as a historical note.Bear in mind that
undefined
is (unfortunately) not a reserved word in JavaScript if you’re not using strict mode. Therefore, someone (someone else, obviously) could have the grand idea of redefining it, breaking your code.A more robust method is therefore the following:
On the flip side, this method is much more verbose and also slower. :-/
A common alternative is to ensure that
undefined
is actually undefined, e.g. by putting the code into a function which accepts an additional parameter, calledundefined
, that isn’t passed a value. To ensure that it’s not passed a value, you could just call it yourself immediately, e.g.:You can also use the ES6
Reflect
object:Documentation on MDN for
Reflect.has
can be found here.You can use the
in
operator to check if the property exists on an object:You can also loop through all the properties of the object using a
for - in
loop, and then check for the specific property:You must consider if this object property is enumerable or not, because non-enumerable properties will not show up in a
for-in
loop. Also, if the enumerable property is shadowing a non-enumerable property of the prototype, it will not show up in Internet Explorer 8 and earlier.If you’d like a list of all instance properties, whether enumerable or not, you can use
This will return an array of names of all properties that exist on an object.
Finally, you can use the typeof operator to directly check the data type of the object property:
If the property does not exist on the object, it will return the string undefined. Else it will return the appropriate property type. However, note that this is not always a valid way of checking if an object has a property or not, because you could have a property that is set to undefined, in which case, using
typeof x.key
would still return true (even though the key is still in the object).Update: You can check if a property exists by comparing to the undefined javascript property
This should work unless key was specifically set to
undefined
on the x objectYes it is :) I think you can also do
Object.prototype.hasOwnProperty.call(x, 'key')
which should also work ifx
has a property calledhasOwnProperty
:)But that tests for own properties. If you want to check if it has an property that may also be inhered you can use
typeof x.foo != 'undefined'
.