Test if something is not undefined in JavaScript

前端 未结 11 1845
礼貌的吻别
礼貌的吻别 2020-11-30 19:50

I\'m checking if(response[0].title !== undefined), but I get the error:

Uncaught TypeError: Cannot read property \'title\' of undefined.<

11条回答
  •  心在旅途
    2020-11-30 20:15

    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
    }
    

提交回复
热议问题