How to check if a JavaScript variable has actually been declared?
This solution doesn't work in my case:
JavaScript check if variable exists (is defined/initialized)
Example:
(function(){
var abc;
alert(typeof abc === 'undefined') // true
})();
Same as:
(function(){
alert(typeof abc === 'undefined') // true
})();
It produces true
in both cases. I could do this:
(function(){
var abc;
var isDefined = false;
try { isDefined = abc || !abc; }
catch(e) {}
alert(isDefined); // true
})();
It works, but I'm looking for anything better that this, x-browsers.
EDIT: I want to use it in a piece of dynamic code which runs via eval
and checks if a certain variable exists in either local or global scope.