33

How should I test if a variable is defined?

if //variable is defined
    //do this
else
    //do this
boom
  • 9,524
  • 8
  • 39
  • 58

4 Answers4

75
if (typeof variable !== 'undefined') {
  // ..
}
else
{
     // ..
}

find more explanation here:

JavaScript isset() equivalent

Community
  • 1
  • 1
Mithun Satheesh
  • 25,056
  • 14
  • 73
  • 97
  • 3
    Use !== for strict comparison and better performance. – arxpoetica Jan 11 '14 at 15:22
  • 1
    @AmericanYak—in a *typeof* test the algorithm for resolving [`==`](http://ecma-international.org/ecma-262/5.1/#sec-11.9.3) and [`===`](http://ecma-international.org/ecma-262/5.1/#sec-11.9.6) is identical since both values have the same Type (string), so where does the better performance come from? – RobG Feb 13 '14 at 00:51
  • This fails when because `typeof null` is `Object` – nikjohn Aug 30 '16 at 08:34
8

Use the in operator.

'myVar' in window; // for global variables only

typeof checks will return true for a variable if,

  1. it hasn't been defined
  2. it has been defined and has the value undefined, or
  3. it has been defined but not initialized yet.

The following examples will illustrate the second and third point.

// defined, but not initialized
var myVar;
typeof myVar; // undefined

// defined, and initialized to undefined
var myVar = undefined;
typeof myVar; // undefined
RobG
  • 124,520
  • 28
  • 153
  • 188
Anurag
  • 132,806
  • 34
  • 214
  • 257
  • You can use *in window* only in the special case of global variables as they are also made properties of the global object. Local variables have no such accssible variable object. Assuming that there is a window object might be reasonable for browsers but is not neccessarily true in general. – RobG Sep 28 '11 at 06:18
  • 1
    @RobG - it doesn't have to be `window` or the global object. Any object can be used here instead. You've raised a good point about variables defined locally not being accessible here. I'll be interested in finding out if there's a way to determine undefinedness for local variables based only on the first criteria I have. – Anurag Sep 28 '11 at 06:25
  • If you are talking about variables and scope, then you can only distinguish between global variables (as they are properties of the global object) and the rest. In a nested function, it is impossible to tell if an identifier is a local or outer function variable because you can't access the related variable objects. Object property resolution is a different thing. – RobG Sep 28 '11 at 08:21
  • 1
    @RobG it's still the best answer because testing if the variable is merely equal to undefined doesn't mean anything other than it's undefined... but the variable might exists. – Loïc Faure-Lacroix Feb 13 '14 at 00:05
  • @LoïcFaure-Lacroix—there are no good answers here, it is impossible to tell if a local variable "exists" or not. – RobG Feb 13 '14 at 00:58
4

You simply check the type.

if(typeof yourVar !== "undefined"){
  alert("defined");
}
else{
  alert("undefined");
}
Brian Antonelli
  • 739
  • 9
  • 16
0

You can use something like this

if (typeof varname !== 'undefined') {
    // do this
} else {   
    // do that
}
CertainPerformance
  • 260,466
  • 31
  • 181
  • 209
xyz
  • 21,367
  • 33
  • 107
  • 150