224

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

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

SHernandez
  • 1,060
  • 1
  • 14
  • 21
Raimonds
  • 2,606
  • 3
  • 20
  • 25

11 Answers11

344

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
}
gunwin
  • 4,578
  • 5
  • 37
  • 59
amosrivera
  • 26,114
  • 9
  • 67
  • 76
  • 20
    Despite the number of votes, this is not a good way to perform the check because `undefined` is not a JavaScript keyword (although everyone seems to think it is). If you want to, you could create a variable called `undefined` and then this check will be incorrect. The only correct way is to check `(typeof myVar !== 'undefined')`. – BadHorsie Jun 20 '16 at 14:12
  • 4
    @BadHorsie makes sense, I updated the answer with your comment. – amosrivera Jun 20 '16 at 16:39
  • 1
    What if a variable exists and contains the string that reads 'undefined'? – TomeeNS Feb 13 '19 at 15:29
  • 1
    @BadHorsie Your argument is bogus. Why should I redefine `undefined`? – Robert Siemer Feb 15 '20 at 05:15
  • 1
    @amosrivera No, his/her comment did not make sense. You worsened your answer. – Robert Siemer Feb 15 '20 at 05:17
  • 1
    @RobertSiemer Hi Robert. I think it did although he/she is referring to old browser behavior. You can read more about it here: https://www.codereadability.com/how-to-check-for-undefined-in-javascript/ – amosrivera Mar 31 '20 at 00:04
  • @amosrivera My point is: if you actually want to check for undefined do `thing === undefined`. No `typeof`, no `void`.—And I’m convinced that my point was even true with “old browser behavior”, i.e. from the beginning of Javascript. Even the website you mention supports my argument in the first paragraphs, and then it delves into details about what it doesn’t recommend. – Robert Siemer Mar 31 '20 at 05:37
40

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){ ... }
Rion Williams
  • 74,820
  • 37
  • 200
  • 327
35

I had trouble with all of the other code examples above. In Chrome, this was the condition that worked for me:

typeof possiblyUndefinedVariable !== "undefined"

I will have to test that in other browsers and see how things go I suppose.

Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313
Karl Henselin
  • 1,015
  • 12
  • 17
13

Actually you must surround it with an Try/Catch block so your code won't stop from working. Like this:

try{
    if(typeof response[0].title !== 'undefined') {
        doSomething();
    }
  }catch(e){
    console.log('responde[0].title is undefined'); 
  }
Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313
André Luiz
  • 193
  • 2
  • 10
  • I was looking at IE 8 and needed to do a try catch. – RonanOD Dec 30 '13 at 15:28
  • 1
    this is the best way if you are testing if a function is undefined – edepperson Mar 12 '14 at 01:28
  • throwing an error to check undefined is not a good solution. @amosrivera is the best solution. – user3658423 Apr 12 '15 at 05:49
  • @user3658423 : It isn't throwing an error. It only prevents the script from stopping if response is undeclared (because that would throw an error). – André Luiz May 08 '15 at 15:41
  • Sometimes errors can be helpful. With your proposed solution, if doSomething() throws an exception, all you would see in your console is "responde[0].title is undefined", instead of a useful message about an "un-caught exception @ line 123 of file xyz.js". Eventually a second developer will probably comment out the console.log() statement, making it more difficult for a 3rd developer to come in and fix a bug happening deep within doSomething(). – Eric Seastrand Sep 25 '15 at 14:58
  • Yes @Eric you're completely right. However my snippet does not intended to go beyond answering OP's question, which is about checking whether 'something' isn't undefined. In other words, following steps would be to treat, accordingly, whatever error comes inside catch block. – André Luiz Sep 25 '15 at 21:38
9

typeof:

var foo;
if (typeof foo == "undefined"){
  //do stuff
}
maRtin
  • 6,336
  • 11
  • 43
  • 66
1

Check if condition == null; It will resolve the problem

eko
  • 39,722
  • 10
  • 72
  • 98
1

It'll be because response[0] itself is undefined.

DavidGouge
  • 4,583
  • 6
  • 34
  • 46
0

I know i went here 7 months late, but I found this questions and it looks interesting. I tried this on my browser console.

try{x,true}catch(e){false}

If variable x is undefined, error is catched and it will be false, if not, it will return true. So you can use eval function to set the value to a variable

var isxdefined = eval('try{x,true}catch(e){false}')
AngeLOL
  • 106
  • 2
  • 9
0

In some of these answers there is a fundamental misunderstanding about how to use typeof.

Incorrect

if (typeof myVar === undefined) {

Correct

if (typeof myVar === 'undefined') {

The reason is that typeof returns a string. Therefore, you should be checking that it returned the string "undefined" rather than undefined (not enclosed in quotation marks), which is itself one of JavaScript's primitive types. The typeof operator will never return a value of type undefined.


Addendum

Your code might technically work if you use the incorrect comparison, but probably not for the reason you think. There is no preexisting undefined variable in JavaScript - it's not some sort of magic keyword you can compare things to. You can actually create a variable called undefined and give it any value you like.

let undefined = 42;

And here is an example of how you can use this to prove the first method is incorrect:

https://jsfiddle.net/p6zha5dm/

BadHorsie
  • 14,135
  • 30
  • 117
  • 191
0

Check if you're response[0] actually exists, the error seems to suggest it doesn't.

Exelian
  • 5,749
  • 1
  • 30
  • 49
0

You must first check whether response[0] is undefined, and only if it's not, check for the rest. That means that in your case, response[0] is undefined.

Alex Turpin
  • 46,743
  • 23
  • 113
  • 145