If you have a global variable or some property you could use hasOwnProperty method. e.g window.hasOwnProperty('myVar')
or object.hasOwnProperty('myVar')
it will be true - if a variable/property has been defined.
The typeof operator will not give you such information it as it will return 'undefined' for variables that are not defined at all but you can combine it with previous technique to check if a variable is defined yet not assigned a value. But this can still give you false positive if a variable was first assigned a value and the assigned undefined.
For your local variable (non global variables) although they are properties you cannot reference "their" object. But a call to an undefined variable should result in Exception. So you may try this snippet:
function isDefined(variableName) {
try {
eval(variableName);
} catch (ex) {
return false;
}
return true;
}
eval here is used so the exception will happen inside try catch - not when isDefined is called. And I really advise against using this anywhere near production.
All that said what you want is a little strange - do you really need to check that a variable is defined?