How can i check if a particular key exists in a JavaScript array?
Actually, i am checking for undefinedness for whether a key exists. What if the key exists but the value is actually undefined?
var obj = { key: undefined }; obj["key"] != undefined // false, but the key exists!
With in
operator.
0 in [10, 42] // true
2 in [10, 42] // false
'a' in { a: 'foo' } // true
'b' in { a: 'foo' } // false
Use the in
operator.
if ( "my property name" in myObject )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With