190
votes

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; }
5
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.aspdaddywoodland
@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
@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.Marc Stober
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

5 Answers

319
votes

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.

129
votes

if (myVariable === undefined)

or more precisely

if (typeof myVariable === 'undefined')

Note the === is used

3
votes
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.)

2
votes

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; }
-7
votes

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