224

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

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

2

11 Answers 11

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
}
Sign up to request clarification or add additional context in comments.

7 Comments

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 makes sense, I updated the answer with your comment.
What if a variable exists and contains the string that reads 'undefined'?
@BadHorsie Your argument is bogus. Why should I redefine undefined?
@amosrivera No, his/her comment did not make sense. You worsened your answer.
|
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){ ... }

Comments

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.

2 Comments

Same - I found undefined had to be a string.
this must be something new with chrome (i'm on win chrome 41 atm). the accepted answer used to work just fine! :P
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'); 
  }

6 Comments

I was looking at IE 8 and needed to do a try catch.
this is the best way if you are testing if a function is undefined
throwing an error to check undefined is not a good solution. @amosrivera is the best solution.
@user3658423 : It isn't throwing an error. It only prevents the script from stopping if response is undeclared (because that would throw an error).
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().
|
9

typeof:

var foo;
if (typeof foo == "undefined"){
  //do stuff
}

Comments

1

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

Comments

1

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

1 Comment

This should be a comment
0

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

Comments

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.

Comments

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}')

1 Comment

An interesting approach if I might say so myself.
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/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.