-1

Possible Duplicate:
How to check for undefined or null variable in javascript

I want to check for an defined variable in javascript. Please see the following few examples and help me which is the best method to check 'a' for undefined (and check for nothing else) in Javascript?

one

if(a === undefined) { ... }

second

if(a === "undefined") { ... }

third

if(typeof a == "undefined") { ... }

last

if(a) { ... }

Community
  • 1
  • 1
Om3ga
  • 30,465
  • 43
  • 141
  • 221
  • are you checking if variable has been defined or if it is undefined? i.e. variable is defined: var a; variable is undefined: var a; – PitaJ Jul 23 '12 at 03:19

3 Answers3

2

if(typeof a == "undefined") { ... } is the best way to check if a variable is undefined.
if(a === undefined) { ... } is often the same thing, however, contrary to common belief "undefined" is NOT a keyword in javascript, and can in fact have a value assigned to it. Also, if a hasn't been declared or initialized an error will be thrown.

if(a === "undefined") { ... } will check if a is a string with the value of "undefined", and
if(a) { ... } will return true for all falsey values, such as null and 0.

jeff
  • 8,300
  • 2
  • 31
  • 43
  • you said that `if(a === undefined)` is normally the same thing. With which method are you comparing with? Secondly is it correct to say `if(typeof a == "undefined")`or this is correct `if(typeof(a) == "undefined")`? – Om3ga Jul 23 '12 at 03:34
  • `if(a === undefined)` is normally the same thing as `if(typeof a == "undefined")`. `if(typeof a == "undefined")` and `if(typeof(a) == "undefined")` are exactly the same. You can use whichever version you want. – jeff Jul 23 '12 at 03:37
  • @jeff—the first is not a good way to check since if `a` hasn't been declared or initialised an error will be thrown. The best way to check is `if (typeof a == 'undefined')`. Of course that doesn't distinguish between whether `a` has been created and assigned a value of undefined or hasn't been created at all. But then what is the point of that distinction anyway? – RobG Jul 23 '12 at 03:55
  • @RobG - Thanks for informing me about the error being thrown with the first method. I updated my answer. – jeff Jul 23 '12 at 04:02
2
if (typeof a == "undefined") {
    ....
}
RobG
  • 142,382
  • 31
  • 172
  • 209
abhi.gupta200297
  • 881
  • 6
  • 12
2

undefined in javascript is an actual value, so if you do a === "undefined" you're just checking if a variable that is already defined has a value of undefined assigned to it. If the variable isn't hoisted yet then you'll get a nice error.

if(a) is checking if the value of a is true or false. undefined in JS as many other values is falsey so it acts as false. Still, if the variable hasn't been declared before then this will throw an error too.

typeof a === "undefined" is the correct way of checking for a variable that might not exist yet.

elclanrs
  • 92,861
  • 21
  • 134
  • 171
  • why do you think `typeof a === "undefined"` is the cirrect way of checking? – Om3ga Jul 23 '12 at 03:26
  • because it'll actually test if the variable exists or not, not if it's value is `undefined` or `false` like the other options. – elclanrs Jul 23 '12 at 03:26
  • Because it will not throw an error if `a` hasn't been initialised or declared, where as `if (a)` will. – RobG Jul 23 '12 at 03:58
  • @eclans—no, it doesn't. It will return true if **either** a hasn't been or if it has been declared or initialised and assigned the undefined value. – RobG Jul 23 '12 at 04:00