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?<
Three ways to check if a property is present in a javascript object:
!!obj.theProperty
true
for all but the false
valuetheProperty
' in objobj.hasOwnProperty('theProperty')
toString
method, 1 and 2 will return true on it, while 3 can return false on it.)Reference:
http://book.mixu.net/node/ch5.html