views:

57

answers:

5

Possible Duplicate:
Detecting an undefined object property in JavaScript

Would it be like this?

if(variable == undefined) {

or would it be like this?

if(variable == "undefined") {
A: 
if(variable == undefined) {
NicolasT
+5  A: 
if(typeof(variable) == 'undefined')
aadravid
what is typeof?
chromedude
You don’t need the parentheses as `typeof` is an operator and not a function.
Gumbo
@chromedude See here: https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Operators/Special_Operators/typeof_Operator
Tim Goodman
ok thanks, makes sense now
chromedude
ya you are right Gumbo... sorry about that :) was in a hurry when i answered :P
aadravid
+1  A: 

Use the typeof operator here, like this:

if(typeof variable == "undefined") {
Nick Craver
Why would you add an extra call to `typeof` when simply checking `variable == undefined` works?
Justin Niessner
@Justin - `undefined` may be redefined, for example: http://jsfiddle.net/nick_craver/gHjg5/
Nick Craver
A: 

The first one

if (variable == undefined) {}
o6tech
+3  A: 
if (typeof variable == 'undefined')
AJ