224

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

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

SHernandez
1,1302 gold badges14 silver badges21 bronze badges
asked Aug 12, 2011 at 13:56
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
}
gunwin
4,8525 gold badges40 silver badges59 bronze badges
answered Aug 12, 2011 at 13:58
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){ ... }
answered Aug 12, 2011 at 13:58

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.

Roko C. Buljan
209k41 gold badges335 silver badges347 bronze badges
answered Jul 15, 2013 at 17:56

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'); 
 }
Roko C. Buljan
209k41 gold badges335 silver badges347 bronze badges
answered Jul 14, 2013 at 1:34

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
}
maRtin
6,55611 gold badges46 silver badges68 bronze badges
answered May 18, 2015 at 15:51

Comments

1

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

answered Aug 12, 2011 at 13:57

Comments

1

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

eko
40.8k11 gold badges80 silver badges104 bronze badges
answered Dec 24, 2016 at 5:43

1 Comment

This should be a comment
0

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

answered Aug 12, 2011 at 13:57

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.

answered Aug 12, 2011 at 13:58

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}')
answered Jul 25, 2017 at 16:40

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/

answered Feb 16, 2020 at 22:36

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.