Test if something is not undefined in JavaScript
I'm checking if(response[0].title !== undefined)
, but I get the error:
Uncaught TypeError: Cannot read property 'title' of undefined.
response[0]
is not defined, check if it is defined and then check for its property title.
if(typeof response[0] !== 'undefined' && typeof response[0].title !== 'undefined'){
//Do something
}
Just check if response[0]
is undefined:
if(response[0] !== undefined) { ... }
If you still need to explicitly check the title, do so after the initial check:
if(response[0] !== undefined && response[0].title !== undefined){ ... }