Reputation: 20463
Why does an undefined value of an array of array that typeof returns as "undefined" not considered true by my conditional? Is it something to do with the OR operator or what. It seems like my program is running the inner conditional even though it shouldn't be in the inner conditional.
if(typeof elemData !== 'undefined' || typeof rich[elemData.value] !== 'undefined') {
if(typeof rich[elemData.value]['title'] !== 'undefined') {
//do something
}
}
program returns the following:
> if(typeof rich[elemData.value]['title'] !== 'undefined') {
>
> TypeError: Cannot read property 'title' of undefined
I'm checking if rich[elemData.value]
is 'undefined'
and it's saying it's not via my conditional. What's going on?
Upvotes: 0
Views: 57
Reputation: 74645
You are ORing your conditions when you need to AND them.
if(typeof elemData !== 'undefined' || typeof rich[elemData.value] !== 'undefined') {
Should be:
if(typeof elemData !== 'undefined' && typeof rich[elemData.value] !== 'undefined') {
Upvotes: 5