203

How do I find if a variable is undefined?

I currently have:

var page_name = $("#pageToEdit :selected").text();
var table_name = $("#pageToEdit :selected").val();
var optionResult = $("#pageToEditOptions :selected").val();

var string = "?z=z";
if ( page_name != 'undefined' ) { string += "&page_name=" + page_name; }
if ( table_name != 'undefined' ) { string += "&table_name=" + table_name; }
if ( optionResult != 'undefined' ) { string += "&optionResult=" + optionResult; }
4
  • 3
    undefined is a property of javascript so doesn't need to be in quotes. You're checking to see if the values are actually the string 'undefined'. w3schools.com/jsref/jsref_undefined.asp Sep 28, 2009 at 7:44
  • 2
    @daddywoodland: Little advice; you shouldn't be referencing W3Schools. They are known for giving out false information and isn't a good resource to recommend to others.
    – TheCarver
    Feb 28, 2013 at 3:19
  • @PaparazzoKid what is wrong with W3Schools? Do you think the link above is incorrect or do you have other examples? I find the a useful reference, obviously not as authoritative as digging through a W3C document but sometimes you just need to quick reference. Feb 5, 2014 at 23:49
  • I like to use this function below so I can quickly and easily check the existence of a variable. function doesExist(el) { if((typeof el !== "undefined") && (typeof el.val() !== "undefined")){ return true; } else{ return false; } } Then you can just call it like this if (doesExist(variable_name)) { // do stuff }
    – MistyDawn
    Jul 5, 2018 at 17:37

5 Answers 5

328

jQuery.val() and .text() will never return 'undefined' for an empty selection. It always returns an empty string (i.e. ""). .html() will return null if the element doesn't exist though.You need to do:

if(page_name != '')

For other variables that don't come from something like jQuery.val() you would do this though:

if(typeof page_name != 'undefined')

You just have to use the typeof operator.

5
  • 15
    if(undefinedVar) will throw an error, but if(someObj.undefinedProperty) will not. In the latter case, you can also skip typeof and use === undefined (or !==), without quotes. Sep 28, 2009 at 8:56
  • 1
    And I gave you +1 for correctly pointing out that jQuery's methods won't return undefined. Sep 28, 2009 at 8:56
  • Yeah. You're right about the undefined var part.
    – ScottyUCSD
    Sep 28, 2009 at 15:05
  • typeof is helpful to play with self defined variables, we can whether it is defined or not
    – sun
    Jul 24, 2014 at 9:48
  • 1
    if(typeof page_name != 'undefined') should be if(typeof page_name !== 'undefined') Jun 9, 2019 at 4:04
136

if (myVariable === undefined)

or more precisely

if (typeof myVariable === 'undefined')

Note the === is used

7
  • 71
    Please use typeof(var) === 'undefined' as undefined is not a constant in JavaScript. Feb 12, 2012 at 10:21
  • 26
    typeof is not a function - please don't use brackets with it.
    – rjmunro
    Sep 2, 2013 at 13:46
  • 7
    so is this answer valid or not? confused by the upvotes and the comments
    – Roy
    Mar 22, 2016 at 14:46
  • 4
    @Roy You should go for if (typeof var === 'undefined')
    – user6266288
    Sep 26, 2016 at 9:22
  • @JonnyReeves I get the following message when applying typeof(var): Bynary operation argument type string is not compatible with type undefined
    – Pathros
    May 4, 2017 at 15:43
3

http://constc.blogspot.com/2008/07/undeclared-undefined-null-in-javascript.html

Depends on how specific you want the test to be. You could maybe get away with

if(page_name){ string += "&page_name=" + page_name; }
3
function my_url (base, opt)
{
    var retval = ["" + base];
    retval.push( opt.page_name ? "&page_name=" + opt.page_name : "");
    retval.push( opt.table_name ? "&table_name=" + opt.table_name : "");
    retval.push( opt.optionResult ? "&optionResult=" + opt.optionResult : "");
    return retval.join("");
}

my_url("?z=z",  { page_name : "pageX" /* no table_name and optionResult */ } );

/* Returns:
     ?z=z&page_name=pageX
*/

This avoids using typeof whatever === "undefined". (Also, there isn't any string concatenation.)

1
  • 3
    Why is avoiding typeof ... a good thing?
    – rjmunro
    Sep 2, 2013 at 13:46
-7

You can just check the variable directly. If not defined it will return a falsy value.

var string = "?z=z";
if (page_name) { string += "&page_name=" + page_name; }
if (table_name) { string += "&table_name=" + table_name; }
if (optionResult) { string += "&optionResult=" + optionResult; }

Not the answer you're looking for? Browse other questions tagged or ask your own question.