Prometheus
Prometheus

Reputation: 33655

detect an undefined item in jQuery

How do you detect an undefined item in jQuery in an 'if statement' I have tried something like below without any success so far.

 success: function(data){

  if(typeof data.DATA[0].RECIPIENTID = 'undefined'){
     // do nothing                          
   }
   else {
       //else get value
       console.log(data.DATA[0].RECIPIENTID);                       
   }


 console.log(data);

 }
});

Upvotes: 1

Views: 148

Answers (2)

This_is_me
This_is_me

Reputation: 918

undefined does not need quotes ''

    if(RECIPIENTID === undefined){
     // do nothing                          
   alert("undefined");
}

Upvotes: 1

Matt Ball
Matt Ball

Reputation: 359966

= is an assigment operator in JavaScript. Use ===, not =.

if(typeof data.DATA[0].RECIPIENTID === 'undefined'){
    // ...
}
// ...

See also Which equals operator (== vs ===) should be used in JavaScript comparisons?

Upvotes: 9

Related Questions