How do I check if a particular key exists in a Javascript associative array?
If a key doesn't exist and I try to access it, will it return false? Or throw an error?
If a key doesn't exist and I try to access it, will it return false? Or throw an error?
It will return undefined.
var aa = {hello: "world"};
alert( aa["hello"] );      // popup box with "world"
alert( aa["goodbye"] );    // popup boc with "undefined"// note the three equal signs so that null won't be equal to undefined
if( aa["goodbye"] === undefined ) {
    // do something
}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 the in operator// this works even if you have {"goodbye": undefined}
if( "goodbye" in aa ) {
    // do something
} 
Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging…
ReplyDeleteAndroid Online Training
ReplyDeletePretty Post! It is really interesting to read from the beginning & I would like to share your blog to my circles for getting awesome knowledge, keep your blog as updated.
Software Testing Training in Chennai | Software Testing Training in Anna Nagar | Software Testing Training in OMR | Software Testing Training in Porur | Software Testing Training in Tambaram | Software Testing Training in Velachery