202

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; }
Community
  • 1
  • 1
Phil Jackson
  • 10,238
  • 23
  • 96
  • 130
  • 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'. http://www.w3schools.com/jsref/jsref_undefined.asp – daddywoodland Sep 28 '09 at 07: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 '13 at 03: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. – Marc Stober Feb 05 '14 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 05 '18 at 17:37

5 Answers5

327

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.

ScottyUCSD
  • 3,736
  • 1
  • 17
  • 11
136

if (myVariable === undefined)

or more precisely

if (typeof myVariable === 'undefined')

Note the === is used

Adil Malik
  • 6,279
  • 7
  • 48
  • 77
Roger
  • 8,286
  • 17
  • 59
  • 77
  • 71
    Please use `typeof(var) === 'undefined'` as `undefined` is not a constant in JavaScript. – JonnyReeves Feb 12 '12 at 10:21
  • 26
    `typeof` is not a function - please don't use brackets with it. – rjmunro Sep 02 '13 at 13:46
  • 7
    so is this answer valid or not? confused by the upvotes and the comments – Roy Mar 22 '16 at 14:46
  • 4
    @Roy You should go for `if (typeof var === 'undefined')` –  Sep 26 '16 at 09:22
  • @JonnyReeves I get the following message when applying `typeof(var)`: _Bynary operation argument type string is not compatible with type undefined_ – Pathros May 04 '17 at 15:43
  • I upvoted Ascam and was too late to remove my vote :( turns out 'if (var === undefined)' work perfectly I tried using 'typeof' and it did not work. – Orion May 13 '17 at 13:21
  • `if (typeof var === 'undefined')` vs `if (var === undefined)`... Both should work with very subtle differences ***[citation needed]***. Do note the difference of quotes around `undefined` in both use cases which should address the confusion in comments. – Fr0zenFyr Oct 30 '17 at 06:38
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; }
micmcg
  • 2,372
  • 18
  • 16
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.)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
-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; }
Rich Seller
  • 83,208
  • 23
  • 172
  • 177