#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    396
    Rep Power
    48

    Correct way to check if something is undefined?


    I have seen the following code on the net for checking if something is undefined:

    Code:
    if(variable === 'undefined')
    However I tried to use it to check if an email address is undefined with the JavaScript Facebook SDK, and it did not work unless I removed the single quote marks:

    Code:
    FB.api('/me', function (response) {                     
       if (!(response.email === undefined)){
                     
       }
    });
    Why don't quotes work? Am I doing it wrong?
  2. #2
  3. Wiser? Not exactly.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    5,959
    Rep Power
    4035
    You're supposed to use the typeof operator in the quotes version.
    Code:
    if (typeof variable === 'undefined')
    Checking against the true undefined value works ok for object properties, but it would fail if trying to check true variables (you'd get a ReferenceError instead)
    Recycle your old CD's, don't just trash them



    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    22
    Rep Power
    0
    Originally Posted by romario
    I have seen the following code on the net for checking if something is undefined:

    Code:
    if(variable === 'undefined')
    However I tried to use it to check if an email address is undefined with the JavaScript Facebook SDK, and it did not work unless I removed the single quote marks:

    Code:
    FB.api('/me', function (response) {                     
       if (!(response.email === undefined)){
                     
       }
    });
    Why don't quotes work? Am I doing it wrong?

    Your IF statement is OK but the problem is "response" variable is not checked before the if statement.

    Check this example

    http://jsfiddle.net/rVSF7/


    This is what I would do
    Code:
    FB.api('/me', function (response) {
       response = response || {};   //Force the passing variable to be an object
       if (!(response.email === undefined)){
                     
       }
    });
    better yet, this is more efficient approach
    Code:
    FB.api('/me', function (response) {
       response = response || {};   //Force the passing variable to be an object
       if (!response.email){
                     
       }
    });
    !response.email code will make sure that the variable is not null OR undefined. (don't forget, in Javascript null !== undefined)

    NOTE: depends on your application, response = response || {}; might not be the best checking code.
    You might also consider response not being null in your IF statement. like this
    Code:
    FB.api('/me', function (response) {
       if (!response && !response.email){
                     
       }
    });

IMN logo majestic logo threadwatch logo seochat tools logo