6

Both scenario, the typeof the variable will be "undefined". But undeclared variable will raise a exception.

Is there a easy way to handle this?

Lightness Races in Orbit
  • 358,771
  • 68
  • 593
  • 989
leon
  • 2,618
  • 3
  • 19
  • 31
  • 2
    Can you specify some sort of code sample or a more in-depth description in your question? – nmagerko Dec 30 '11 at 03:20
  • 1
    You could use the `in` operator to check for variables defined (with the value `undefined`) in the global scope, or on objects. But it's not possible for local variables created inside a function as they're not accessible programmatically. See my [answer](http://stackoverflow.com/a/3390426/165737) from an older thread. For local variables created inside a function using a `try..catch` block looking for a `ReferenceError` seems to be the only solution as also suggested in @pst's linked answer. – Anurag Dec 30 '11 at 03:22
  • 1
    Why are you trying to use variables that you haven't declared? If you get a `ReferenceError`, that means it's time to fix some code. –  Dec 30 '11 at 03:28
  • @pst: The first sentence indicates that he did, and why it's not appropriate. – Lightness Races in Orbit Dec 31 '11 at 14:26

4 Answers4

4

You may find the question (and my answer) in How to check if a variable or object is undefined? relevant. In general, I view any access to an "undeclared variable" a programming error.

However, this particular case can *only** be detected with the use of detecting for a ReferenceError exception. But, yuck, yuck, yuck! Remember variable declarations are a static lexical construct, ignoring the quirks with property-variables of the global object.

ReferenceError, and now "strict", exist for a reason and I suspect this is an X-Y problem. I do not even recommend the use of typeof for this purpose: fix the code :-)

Happy coding.


*It has been pointed out that "variable" in window will also [and only] work for global "variables" (they are really just properties that don't need be qualified in all contexts).

Community
  • 1
  • 1
1

if property we want to check in object whether that it exists or not, even if its undefined.

we will use one of these: 'prop' in obj(to check for properties from prototype chain) or obj.hasOwnProperty('prop')

we need to use methods above to check if property exists as accessing property that has not been declared in object will also return undefined.

var o={};
o.c=undefined;
o.c===undefined; //is true
o.a===undefined; //is true as well even though c exists while a doesn't

commonly not a problem as nobody really declare undefined properties much, but when do so do it like this.

o.c=''; //when it can be string or 
o.c=null; //to clearly indicate that its nothing.
then
o.c === undefined will return false!
note!!!
null ==  undefined //true while
null === undefined //false that's why use three equals to test

For variables not declared and are not inside object. When accessed The compiler will return (reference)error. If it doesn't it means its being treated as a global property, window object property, and was not declared, at least in all parent scope, so it will be undefined just as o.a was at top. it will become window.prop.

so x; //error
but x=3; //no error assumed to be global object.
just like o.abcd = 3; would...
make(declare) a property abcd in object o valued(assigned) 3 all at once.

To avoid properties to become a global variable we use var keyword inside function, like this var k;

One thing you can do about this catch the reference error when throw for a variable that doesn't exist and are thought to be a variable itself.

try {
x
} catch(e){//code to run when x is not declared let alone defined.}
Muhammad Umer
  • 1,960
  • 2
  • 15
  • 17
0

You can try:

var a;
try {
    a;
    alert('a');
} catch(e) { /* a not defined */ }
try {
    b;
    alert('b');
} catch(e) { /* b not defined */ }
alert('done');

DEMO

qwertymk
  • 30,576
  • 24
  • 107
  • 179
0

You should never be attempting to access undeclared vars if you're writing clean JS. To avoid such pitfalls (among many others) start LINTing your JS with http://www.jslint.com/ or http://jshint.com/ .

A good read to help you understand the LINT tools and reasoning behind their findings is Crockford's Book, JavaScript: The Good Parts ( http://www.amazon.com/gp/aw/d/0596517742 ).

JAAulde
  • 18,107
  • 5
  • 49
  • 59