Test if something is not undefined in JavaScript

JavascriptUndefined

Javascript Problem Overview


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

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

Javascript Solutions


Solution 1 - Javascript

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
}

Solution 2 - Javascript

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){ ... }

Solution 3 - Javascript

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.

Solution 4 - Javascript

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

Solution 5 - Javascript

typeof:

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

Solution 6 - Javascript

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

Solution 7 - Javascript

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

Solution 8 - Javascript

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

Solution 9 - Javascript

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.

Solution 10 - Javascript

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

Solution 11 - Javascript

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/

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRaimondsView Question on Stackoverflow
Solution 1 - JavascriptamosriveraView Answer on Stackoverflow
Solution 2 - JavascriptRion WilliamsView Answer on Stackoverflow
Solution 3 - JavascriptKarl HenselinView Answer on Stackoverflow
Solution 4 - JavascriptAndré LuizView Answer on Stackoverflow
Solution 5 - JavascriptJeffrey Mark BaldridgeView Answer on Stackoverflow
Solution 6 - JavascriptDavidGougeView Answer on Stackoverflow
Solution 7 - JavascriptTanmay ChandraView Answer on Stackoverflow
Solution 8 - JavascriptExelianView Answer on Stackoverflow
Solution 9 - JavascriptAlex TurpinView Answer on Stackoverflow
Solution 10 - JavascriptAngeLOLView Answer on Stackoverflow
Solution 11 - JavascriptBadHorsieView Answer on Stackoverflow